Stop animation and start transition on hover

前端 未结 6 1347
误落风尘
误落风尘 2021-01-07 19:05

I have a link that\'s running an infinite animation with the background color. I want to stop the animation and transition into a different background color on hover.

<
相关标签:
6条回答
  • 2021-01-07 19:09

    I was trying to achieve the same kind of thing and after trying to dynamically change keyframes and all, I found a weird solution by using basic css, see fiddle here. It is not very elegant but does exactly what I (and you, I hope) want.

    #menu, #yellow{
      position: fixed;
      top: 2.5vw;
      right: 2.5%;
      height: 25px;
      width: 25px;
      border-radius: 30px;
    }
    
    #menu{
      animation: blink 2s infinite;
    	transition: 1s;
    }
    
    @keyframes blink{
      0% { background-color: grey; }
      50% { background-color: black; }
      100% { background-color: grey; }
    }
    
    #yellow{
    	background-color: rgba(255, 0, 0, 0);
    	transition: 1s;
    }
    
    #disque:hover #yellow{
    	pointer-events: none;
    	background-color: rgba(255, 0, 0, 1);
    }
    
    #disque:hover #menu{
    	opacity: 0;
    }
    <div id="disque">
      <div id="menu"></div>
      <div id="yellow"></div>
    </div>

    0 讨论(0)
  • 2021-01-07 19:13

    For those who are interested by animation slide with stop between 2 images

    var NumImg = 1; //Img Number to show
    var MaxImg = 3; //How many Img in directory ( named 1.jpg,2.jpg ...)
    
    function AnimFond() {
      NumImg = NumImg> MaxImg ? 1 : NumImg +=1;
      var MyImage = "http://startinbio.com/Lib/Images/Fond/" + NumImg + ".jpg";
      $("#ImgFond1").attr("src", MyImage);
      $("#ImgFond2").fadeOut(3000, function() {
        $("#ImgFond2").attr("src", MyImage);
        $("#ImgFond2").fadeIn(1);
      });
    }
    
    setInterval("AnimFond()", 10000); //delay between 2 img
    #AnimFond {
      position: fixed;
      height: 100%;
      width: 100%;
      margin: 0 0 0 -8;
    }
    
    #AnimFond img {
      position: absolute;
      left: 0;
      height: 100%;
      width: 100%;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
    <div id="AnimFond">
      <img id="ImgFond1" src="http://startinbio.com/Lib/Images/Fond/1.jpg" />
      <img id="ImgFond2" src="http://startinbio.com/Lib/Images/Fond/1.jpg" />
    </div>

    0 讨论(0)
  • 2021-01-07 19:20

    Found another way round to achieve this.

    Write another animation keyframe sequence and call it on your hover.

    .startlink{
    background-color:#206a9e;
    color:#fff;
    border-radius:15px;
    font-family: 'Myriad Pro';
    -webkit-animation:changeColor 3.4s infinite;
    -webkit-transition:all 0.2s ease-in;
    }
    
    .startlink:hover{
    -webkit-animation:hoverColor infinite;
    }
    
    @-webkit-keyframes changeColor 
    {
    0%   {background:#206a9e;}
    50%  {background:#012c4a;}
    100%   {background:#206a9e;}
    }
    @-webkit-keyframes hoverColor 
    {
    background: #014a2a;
    }
    
    0 讨论(0)
  • 2021-01-07 19:23

    I have the same issue and the solution I found is the following.

    Create the animation you want and for the element you and to each assign each one a different class.

    Then use .mouseover() or .mouseenter() jQuery events toggle between the classes you assigned to each animation.

    It is similar to what you use for a burger menu, just with a different handler.

    0 讨论(0)
  • 2021-01-07 19:26

    Try -webkit-animation: 0;. Demo here. 0 is the default value for animation or what you must set to disable any existing CSS3 animations.

    0 讨论(0)
  • 2021-01-07 19:31

    -webkit-animation-play-state: paused and -webkit-animation-play-state: running

    0 讨论(0)
提交回复
热议问题