基於mootools 1.3架構下的圖片滑動效果代碼

來源:互聯網
上載者:User

效果預覽如下:

實現原理:
容器採用相對定位,圖片採用絕對位置,當滑鼠移動到相應的圖片上,改變去left屬性,用tween實現動畫效果.

程式碼分析:寫一個picSlider類實現代碼封裝
複製代碼 代碼如下:
<div id="container">
<img src="http://files.jb51.net/file_images/article/201104/r_song1.jpg" alt="" />
<img src="http://www.bkjia.com/uploads/allimg/131016/1HK51S7-2.jpg" alt="" />
<img src="http://www.bkjia.com/uploads/allimg/131016/1HK55U8-3.jpg" alt="" />
<img src="http://www.bkjia.com/uploads/allimg/131016/1HK55938-4.jpg" alt="" />
<img src="http://www.bkjia.com/uploads/allimg/131016/1HK53647-5.jpg" alt="" />
</div>

CSS樣式
複製代碼 代碼如下:
#container{width:459px; height:200px; background-color:Black;position:relative;overflow:hidden;}
#container img{position:absolute; width:360px;height:300px;display:block;top:0;width:280px;height:200px;}

JS:picSlider類
複製代碼 代碼如下:
var picSlider = new Class({
Implements: Options,
options: {
container: "container",
imgsWidth: 0.6,
},
initialize: function (options) {
this.setOptions(options);
this.container = $(this.options.container);
this.triggers = this.container.getElementsByTagName("img");
this.containerWidth = this.container.getSize().x; //get container's width
this.imgWidth = this.containerWidth * this.options.imgsWidth;
this.aveWidth = this.containerWidth / this.triggers.length;
this.newAveWidth = (this.containerWidth - this.imgWidth) / (this.triggers.length - 1);
this.setImgsInit(); //初始化圖片展示
this.start();
},
setImgsInit:function(){
for(var i=0;i<this.triggers.length;i++){
this.triggers[i].setStyle("left",i*this.aveWidth);
}
},
start:function(){
for(var i=0;i<this.triggers.length;i++){
this.triggers[i].set("tween",{property:"left",duration:300, fps:80}); //為每個元素設定動畫參數
this.triggers[i].addEvent("mouseover",this.slider.bindWithEvent(this,[i]));//綁定slider函數
}
},
slider:function(e,at){
e.stop();
for(var i=1;i<this.triggers.length;i++){
if(i<=at){
this.triggers[i].get("tween").start(i*this.newAveWidth);
}else{
this.triggers[i].get("tween").start(this.imgWidth+(i-1)*this.newAveWidth);
}
}
}
});
new picSlider();


如果想直接在本地運行,請引入
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/mootools/1.3.1/mootools-yui-compressed.js"></script>
<script type="text/javascript" src="home.js"></script>這個指令碼必須在<div><div>後面,原因不解釋!

相關關鍵詞:
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.