基于mootools 1.3框架下的圖片滑動(dòng)效果代碼
更新時(shí)間:2011年04月22日 01:27:26 作者:
容器采用相對(duì)定位,圖片采用絕對(duì)定位,當(dāng)鼠標(biāo)移動(dòng)到相應(yīng)的圖片上,改變?nèi)eft屬性,用tween實(shí)現(xiàn)動(dòng)畫效果.
效果預(yù)覽如下:

實(shí)現(xiàn)原理:
容器采用相對(duì)定位,圖片采用絕對(duì)定位,當(dāng)鼠標(biāo)移動(dòng)到相應(yīng)的圖片上,改變?nèi)eft屬性,用tween實(shí)現(xiàn)動(dòng)畫效果.
代碼分析:寫一個(gè)picSlider類實(shí)現(xiàn)代碼封裝
<div id="container">
<img src="http://img.jbzj.com/file_images/article/201104/r_song1.jpg" alt="" />
<img src="http://img.jbzj.com/file_images/article/201104/r_song2.jpg" alt="" />
<img src="http://img.jbzj.com/file_images/article/201104/r_song3.jpg" alt="" />
<img src="http://img.jbzj.com/file_images/article/201104/r_song4.jpg" alt="" />
<img src="http://img.jbzj.com/file_images/article/201104/r_song5.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}); //為每個(gè)元素設(shè)置動(dòng)畫參數(shù)
this.triggers[i].addEvent("mouseover",this.slider.bindWithEvent(this,[i]));//綁定slider函數(shù)
}
},
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();
如果想直接在本地運(yùn)行,請引入
<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>這個(gè)腳本必須在<div><div>后面,原因不解釋!

實(shí)現(xiàn)原理:
容器采用相對(duì)定位,圖片采用絕對(duì)定位,當(dāng)鼠標(biāo)移動(dòng)到相應(yīng)的圖片上,改變?nèi)eft屬性,用tween實(shí)現(xiàn)動(dòng)畫效果.
代碼分析:寫一個(gè)picSlider類實(shí)現(xiàn)代碼封裝
復(fù)制代碼 代碼如下:
<div id="container">
<img src="http://img.jbzj.com/file_images/article/201104/r_song1.jpg" alt="" />
<img src="http://img.jbzj.com/file_images/article/201104/r_song2.jpg" alt="" />
<img src="http://img.jbzj.com/file_images/article/201104/r_song3.jpg" alt="" />
<img src="http://img.jbzj.com/file_images/article/201104/r_song4.jpg" alt="" />
<img src="http://img.jbzj.com/file_images/article/201104/r_song5.jpg" alt="" />
</div>
CSS樣式
復(fù)制代碼 代碼如下:
#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類
復(fù)制代碼 代碼如下:
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}); //為每個(gè)元素設(shè)置動(dòng)畫參數(shù)
this.triggers[i].addEvent("mouseover",this.slider.bindWithEvent(this,[i]));//綁定slider函數(shù)
}
},
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();
如果想直接在本地運(yùn)行,請引入
<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>這個(gè)腳本必須在<div><div>后面,原因不解釋!
相關(guān)文章
使用Mootools動(dòng)態(tài)添加Css樣式表代碼,兼容各瀏覽器
這個(gè)函數(shù)很有用處,尤其是當(dāng)我們在使用Mootools開發(fā)插件的時(shí)候,例如Tips等,當(dāng)我們需要額外的Css來支撐插件內(nèi)的效果時(shí),通常我們的做法是把Css放到單獨(dú)的css文件里調(diào)用2011-12-12MooTools 頁面滾動(dòng)浮動(dòng)層智能定位實(shí)現(xiàn)代碼
MooTools 頁面滾動(dòng)浮動(dòng)層智能定位實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-08-08Mootools 1.2教程 設(shè)置和獲取樣式表屬性
今天,我們來看一下如何通過MooTools 1.2和我們以前幾講中的內(nèi)容來操作樣式,這將給你在UI上帶來很大的控制權(quán)。2009-09-09基于mootools插件實(shí)現(xiàn)遮罩層新手引導(dǎo)
公司項(xiàng)目有這個(gè)需求,剛好這段時(shí)間在學(xué)習(xí)了mootools,于是把功能寫成了mootools插件,個(gè)人感覺mootools在這方面比jquery強(qiáng)多了2012-05-05分享一個(gè)用Mootools寫的鼠標(biāo)滑過進(jìn)度條改變進(jìn)度值的實(shí)現(xiàn)代碼
分享一個(gè)用Mootools寫的鼠標(biāo)滑過進(jìn)度條改變進(jìn)度值的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-12-12Mootools 1.2教程 同時(shí)進(jìn)行多個(gè)形變動(dòng)畫
今天我們來學(xué)習(xí)一下Fx.Elements插件,這是一個(gè)基于Fx.Morph的插件。與應(yīng)用于單個(gè)元素不同的是,F(xiàn)x.Elements可以允許你一次性給多個(gè)元素添加動(dòng)畫。2009-09-09Mootools 1.2教程 滾動(dòng)條(Slider)
到現(xiàn)在為止,初始化這些MooTools插件對(duì)象就會(huì)開始變得越來越熟悉。滾動(dòng)條(Slider)沒有任何不同,你要?jiǎng)?chuàng)建一個(gè)新的滾動(dòng)條,定義滾動(dòng)條和滑塊相關(guān)的元素,然后設(shè)置你的選項(xiàng),再創(chuàng)建一些回調(diào)事件的控制函數(shù)。2009-09-09