【问题标题】:onmouseenter KeyFrame only works one timeonmouseenter KeyFrame 只能工作一次
【发布时间】:2015-09-01 14:21:19
【问题描述】:

正如您所看到的,当您第一次将鼠标悬停在正方形上时,以下动画会起作用,但在第一次之后就不再起作用了,这就是我的目标:

function js1(x){
x.style.animation="anime 1s";
}

function js2(x){
x.style.animation="anime1 1s";
}
#tria {
width: 0;
height: 0;
border-style: solid;
border-width: 50px 0 50px 200px;
border-color: transparent transparent transparent #007bff;
position:absolute;
top:200px;
}

#tria2 {
width: 0;
height: 0;
border-style: solid;
border-width: 50px 200px 50px 0;
border-color: transparent #007bff transparent transparent;
position:relative; 
float:right;
top:200px;
}

@Keyframes anime{
    from {left: 0;}
    to {left: 200px;}
}
@Keyframes anime1{
    from {left: 0;}
    to {left: -200px;}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Site 2</title>
<link href="styles.css" rel="stylesheet" type="text/css">
<script src="triascript.js"></script>
</head>

<body>

<div id="tria" onmouseenter="js1(this)"></div>
<div id="tria2" onmouseenter="js2(this)" ></div>






</body>
</html>

其他例子:

https://jsfiddle.net/sergiureznicencu/4dyq1dgk/

【问题讨论】:

    标签: css animation keyframe


    【解决方案1】:

    尝试在 mouseleave 之后重置它:

    JS:

    function reset(x){
        x.style.animation = "";
    }
    

    HTML:

    <div id="tria" onmouseenter="js1(this)" onmouseleave="reset(this)"></div>
    <div id="tria2" onmouseenter="js2(this)" onmouseleave="reset(this)"></div>
    

    另一种方法是timeOut:

    JS

    function js1(x){
        x.style.animation="anime 1s";
        setTimeout(function() {x.style.animation = "";}, 1000);
    }
    
    function js2(x){
        x.style.animation="anime1 1s";
        setTimeout(function() {x.style.animation = "";}, 1000);
    }
    

    HTML:

    <div id="tria" onmouseenter="js1(this)"></div>
    <div id="tria2" onmouseenter="js2(this)" ></div>
    

    https://jsfiddle.net/4dyq1dgk/4/

    【讨论】:

      猜你喜欢
      • 2018-01-28
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-04
      相关资源
      最近更新 更多