jquery實現滑動圖片自己測試的例子

來源:互聯網
上載者:User

最近正在回顧之前的一些基礎知識,用jquery寫了一個圖片滑動的外掛程式,雖然還是有些問題存在,用法很簡單,只要在對目標元素繫結slideW()函數即可,該函數最多支援兩個輸入,分別是圖片變化之後的寬度,以及動畫的速度,如果不輸入,則採用預設的寬度以及預設的速度。

下面是我自己測試的例子:
複製代碼 代碼如下:
//html代碼
<div class = "content">
<ul class = "list">
<li class = "liimg"><span class = "imgli img1"></span></li>
<li class = "liimg"><span class = "imgli img2"></span></li>
<li class = "liimg"><span class = "imgli img3"></span></li>
<li class = "liimg"><span class = "imgli img4"></span></li>
</ul>
</div>

css代碼
複製代碼 代碼如下:
//css代碼
*{margin:0px;padding:0px;list-style-type:none;}
body{text-align:center;}
.content{width:590px;margin:0 auto;text-align:left;}
.list{width:565px;margin:0px 9px;border:1px solid #eee;padding:5px;display:inline-block;overflow:hidden;}
.liimg{padding:4px;border:1px solid #aaa;border-radius:2px;display:inline-block;*display:inline;*margin:0px 3px;background:#FFF;width:auto;}
.imgli{margin:2px 0px;width:125px;height:300px;display:inline-block;}
.img1{background:url(images/img1.jpg) no-repeat 50% 50%;}
.img2{background:url(images/img2.jpg) no-repeat 50% 50%;}
.img3{background:url(images/img3.jpg) no-repeat 50% 50%;}
.img4{background:url(images/img4.jpg) no-repeat 50% 50%;}

js代碼
複製代碼 代碼如下:
//js代碼
//jquery實現圖片滑動效果
var zyljq = $.noConflict();
zyljq.fn.extend({
slideW:function(wid,speed){
//wid要設定的寬度,可以設定變得最小時的寬度或者變得最大時的寬度
//如果不設定,則會根據繫結元素的寬度,計算設定預設寬度。
//speed要設定的動畫運行速度,可以是normal,slow,fast,也可以是毫秒數。
//如果不設定,則預設速度是normal
that = this; //綁定該事件的所有元素
var eleNum = zyljq(this).size(); //擷取圖片的數量
var curwidth = zyljq(this).width();//擷取圖片的原始寬度
if(!wid){
wid = Math.round(curwidth*4/5);
//如果沒有設定動畫的終止條件,則計算出預設的情況
}
if(zyljq.trim(speed) == ""){
//如果沒有設定速度,則取預設速度
speed = "normal";
}
if(!isNaN(speed)){
//如果設定了負值,則變化
speed = speed < 0?-speed:speed;
}
//計算照片的最大和最小寬度
if(wid > curwidth){
widMin = Math.floor((eleNum*curwidth - wid)/(eleNum-1))+"px";
widMax = ((curwidth*eleNum) - widMin*(eleNum-1))+"px";
}else{
widMax = Math.floor((eleNum*curwidth - wid*(eleNum-1)))+"px";
widMin = wid+"px";
}
//console.log("widMax="+widMax+";widMin="+widMin);
//綁定hover事件
zyljq(this).hover(function(){
zyljq(that).stop();
zyljq(this).animate({width:widMax},speed);
zyljq(that).not(this).animate({width:widMin},speed);
},function(){
zyljq(that).stop();
zyljq(that).animate({width:curwidth},speed);
});
}
});

下面是視圖:
原始效果:
 
滑鼠移至上方在第二個元素上之後的效果:
 
在這個過程中,也回顧了一些常見的問題,比如:ie低版本下,margin:0 auto;不置中的問題,display:inline-block;不支援的問題。也許還有很多的問題沒有注意到,如果您發現了什麼問題,歡迎您的指導。大家可以共同進步。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.