animate 實現(xiàn)滑動切換效果【實例代碼】
今天和大家分享一下用 animate 實現(xiàn)滑動切換效果的小例子
大家都知道jQuery 提供的有一下幾種方法能夠?qū)崿F(xiàn)滑動效果:
1.slideDown()
2.slideUp()
3.slideToggle()
但是以上的滑動不太方便控制其滑動的方向,所以我們還是自己動手寫一個吧。。。
其代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">
<style type="text/css">
body{
width: 100%;
height: auto;
}
.content{
width: 150px;
height: 50px;
position: absolute;
top: 20px;
left: 20px;
overflow: hidden;
background-color: red;
}
.slide-box{
width: 300px;
position: relative;
}
.slide1{
width: 150px;
height: 50px;
float: left;
display: inline-block;
line-height: 50px;
text-align: center;
background-color: #BDD8CF;
}
.slide2{
width: 150px;
height: 50px;
float: right;
display: inline-block;
line-height: 50px;
text-align: center;
background-color: #C1C4C4;
}
</style>
</head>
<body>
<div class="content">
<div class="slide-box clearfix">
<span class="slide1">左邊的元素</span>
<span class="slide2">右邊的元素</span>
</div>
</div>
<script src="js/jquery-1.11.2.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$(function(){
$(".content").hover(function(){
$(".slide-box").stop(true).animate({right:"150px"},'slow');
},function(){
$(".slide-box").stop(true).animate({right:"0"},'slow');
});
})
</script>
</body>
</html>
以上代碼即可以實現(xiàn)一個完整的滑動效果。但是有一點需要注意,

如上圖所示,需要加上 stop() 事件 ,防止鼠標快速移動時產(chǎn)生的多個事件,形成一個棧隊,造成鼠標移除后依舊滑動甚至閃動的效果。
以上這篇animate 實現(xiàn)滑動切換效果【實例代碼】就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery live( type, fn ) 委派事件實現(xiàn)
jQuery 1.3中新增的方法。給所有當前以及將來會匹配的元素綁定一個事件處理函數(shù)(比如click事件)。也能綁定自定義事件。2009-10-10
jQuery Validation Plugin驗證插件手動驗證
jquery.validate是jquery旗下的一個驗證框架,借助jquery的優(yōu)勢,我們可以迅速驗證一些常見的輸入,并且可以自己擴充自己的驗證方法,并且對國際化也有很好的支持,接下來通過本文給大家介紹jQuery Validation Plugin驗證插件手動驗證2016-01-01
jQuery中對未來的元素綁定事件用bind、live or on
這篇文章主要介紹了jQuery中對未來的元素綁定事件用bind、live or on,需要的朋友可以參考下2014-04-04

