Animation stop with css3

2019-02-25 23:32发布

At the moment i am working on a header with a slider animation (css3 only):

http://jimmytenbrink.nl/slider/

Everything is working fine except sometimes the slider is bugging if you go from the center to the right. It seems that i need to stop the animation for a few miliseconds to complete. However i searched everywhere on the internet but i cant seem to get it to work.

Anyone here has experience with it who can help me out?

HTML

<header>
  <div><span>slide 1</span></div>
  <div><span>slide 2</span></div>
  <div><span>slide 3</span></div>
  <div><span>slide 4</span></div>
  <div><span>slide 5</span></div>
  <div><span>slide 6</span></div>
  <div><span>slide 7</span></div>
  <div><span>slide 8</span></div>    
</header>

CSS

header {
    margin-top: 10px;
    width: 800px;
    overflow: hidden;
    height: 500px;
}
header div {
    background-color: #000;
    width: 43.8px;
    height: 200px;
    position: relative;
    float: left;
    -webkit-transition: width .3s;
    transition: width .3s;
    overflow: hidden;
    -webkit-animation-timing-function: linear;
    -webkit-animation-iteration-count: 1;
    -webkit-animation-fill-mode: forwards;
    margin-right: 2px;
}
header div:first-child {
    margin-left: 0px;
}
header div:last-child {
    margin-right: 0px;
}
header div:hover span {
    left: 50px;
    opacity: 1;
}
header div img {
    position: relative;
    left: -240px;
    -webkit-transition: all .3s;
    transition: all .3s;
    -webkit-filter: grayscale(1);
    overflow:hidden;
}
header div span {
    -webkit-transition: left .3s;
    transition: left .3s;
    position: absolute;
    bottom: 30px;
    color: white;
    left: -350px;
    opacity: 0;
    width: 450px;
    font-family:'Fugaz One', cursive;
    text-transform: uppercase;
    font-size: 24px;
    color: #fff;
    text-shadow: 0px 0px 10px #f1f1f1;
    filter: dropshadow(color=#f1f1f1, offx=0, offy=0);
}
header:hover > div {
    width: 43.8px;
}
header:hover > div:hover {
    width: 150px;
}

Here is a JSFiddle

So the question is, how can i set a stop on the animation for a few miliseconds so the animation can finish before it gets triggered again?

Hope my question is clear!

(thanks for the edit)

2条回答
爷的心禁止访问
2楼-- · 2019-02-26 00:26

One might call my answer a workaround. Maybe it is but according to my comment on ExtPro's answer - it is still completely pure CSS.
I decided to use display: table-cell since the table cell's width is distributed equally.

So, the CSS might look like this:
HINT: This is only a bunch of necessary CSS. All the code is in the jsFiddle

header {
    width: 368px;
    display: table;
    overflow: hidden;
}
header > div {
    width: 44px;
    height: 200px;
    position: relative;
    -webkit-transition: width .3s;
    transition: width .3s;
    display: table-cell;
    overflow: hidden;
}
header > div:hover {
    width: 151px;
}

Fiddle

As you can see, we don't have to determine the width of all not-hovered divs. Actually, the problem came from that very CSS rule:

/* DON'T USE THIS RULE - IT'S THE RULE WHICH WAS BAD */
header:hover > div {
    width: 43.8px;
}

You were changing the width of the divs on header:hover, so when the transition didn't manage to do its job in time, you came out with mouse pointing to the header but to non of the divs.

查看更多
再贱就再见
3楼-- · 2019-02-26 00:31

If I understand what you mean by 'bugging', what is happening is if you move the mouse quickly to the right, it traverses the currently open div and is left in an area which when that div collapses, does not contain (e.g. the mouse is not hovered over) the next one in order to expand it- namely the hover event of the following div(s) is/are not firing thus they do not expand. There wont be a CSS fix for this Im afraid as its browser related, you may want to replace with jQuery/JS.

查看更多
登录 后发表回答