有没有一种方法可以在悬停效果上添加延迟?
Is there a way to add a delay on a hover efect?
我想要完成的是,当图像在悬停后发生变化时,它会保持几秒钟,然后它会返回到原始图像。
我想知道的是是否有办法增加这种延迟。我在下面附上了我的代码。
<html>
<body>
<img src='http://www.freedigitalphotos.net/images/img/homepage/87357.jpg'
width='142' height='162'
onmouseover="this.src='http://7606-presscdn-0-74.pagely.netdna-cdn.com/wp-content/uploads/2016/03/Dubai-Photos-Images-Oicture-Dubai-Landmarks-800x600.jpg';"
onmouseout="this.src=http://7606-presscdn-0-74.pagely.netdna-cdn.com/wp-content/uploads/2016/03/Dubai-Photos-Images-Oicture-Dubai-Landmarks-800x600.jpg';" />
</body>
</html>
使用transition-delay
属性的CSS转场
.classname {
width: 100px;
height: 100px;
background-color: red;
transition-property: background-color;
transition-delay: 1s;
transition-duration: 0.1s;
}
.classname:hover {
transition-delay: 0s;
background-color: blue;
}
.image {
width: 142px;
height: 162px;
background-image: url('http://www.freedigitalphotos.net/images/img/homepage/87357.jpg');
background-size: 100% 100%;
transition-property: background-image;
transition-delay: 1s;
transition-duration: 0.1s;
}
.image:hover {
transition-delay: 0s;
background-image: url('http://7606-presscdn-0-74.pagely.netdna-cdn.com/wp-content/uploads/2016/03/Dubai-Photos-Images-Oicture-Dubai-Landmarks-800x600.jpg')
}
<div class="classname"></div>
<div class="image"></div>
将onmouseout
事件更改为使用setTimeout
调用JS函数
setTimeout(function(){
this.src= "...";
}, 5000);
其中5000是要延迟的时间,以毫秒为单位
你可以直接使用CSS转场。
.button {
background-color: #222;
color: #fff;
padding: 14px 36px;
text-decoration: none;
transition: 0.6s background-color ease
}
.button:hover {
background-color: #555;
}
<a href='#' class='button'>Hover me</a>
看这个例子,用onmouseover
事件改变<img>
src
,等待3秒,然后回到原始图像onmouseout
//copy original img to variable
var original = $("img")[0].src;
//mouse over event
$("img").mouseover(function() {
$(this).fadeOut("fast").fadeIn("fast");
//change image
$(this)[0].src = "http://7606-presscdn-0-74.pagely.netdna-cdn.com/wp-content/uploads/2016/03/Dubai-Photos-Images-Oicture-Dubai-Landmarks-800x600.jpg";
});
//mouse out event
$("img").mouseout(function() {
var img = $(this);
//on mouse over wait 3 second and getback to original img
setTimeout(function() {
img.fadeOut("fast").fadeIn("fast");
img[0].src = original;
}, 3000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img src='http://www.freedigitalphotos.net/images/img/homepage/87357.jpg' width='142' height='162' />
有几种方法可以做到这一点。您可以尝试下面的代码片段:
<div>
<img src='http://7606-presscdn-0-74.pagely.netdna-cdn.com/wp-content/uploads/2016/03/Dubai-Photos-Images-Oicture-Dubai-Landmarks-800x600.jpg' width='142' height='162'/>
<img src='http://www.freedigitalphotos.net/images/img/homepage/87357.jpg' width='142' height='162'/>
</div>
div{
width:142px;
height:162px;
overflow: hidden; /*required*/
}
div img{
position: absolute;
transition: opacity .5s ease;
transition-delay: .5s; /*time of transition that you want*/
}
div img:hover{
opacity: 0;
}
另一种方法是使用这些图像的背景并管理每个图像。
完整示例:jsbin
相关文章:
- 添加一个setTimeout方法会使jQuery悬停方法忽略setTimeout方法中的函数
- 将鼠标悬停在Angular JS中的一个元素上,并将Class添加到另一个元素中
- jQuery在悬停时只添加一个类
- 添加:在悬停其他elem时,将悬停状态添加到不同elem
- 将href链接添加到通过js鼠标悬停显示的图像
- IMG悬停添加样式
- CSS 在悬停时使图像变暗 - 在其周围添加一个 href
- 如何添加效果,以便将鼠标悬停在 SVG 元素上使其变大
- 将鼠标悬停在一个元素上,可以将类添加到另一个元素中
- hover添加类-或子类:悬停以影响父类
- 使用JQuery添加css属性悬停
- 悬停添加类在Chrome上出现故障,但在Firefox上工作正常
- 使用Javascript向鼠标悬停添加延迟
- 为鼠标悬停添加淡入淡出效果
- 当我在智能手机上显示时,将悬停添加到我的页面
- 如何在angularjs中为元素的悬停添加延迟?
- 如何为jquery鼠标悬停添加延迟
- JSF将悬停添加到面板中
- PHP页面-将弹出/悬停添加到调查邀请中
- 将鼠标悬停添加到当前导航栏