源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
楼主: ttx9n

[JavaScript] 基于mootools 1.3框架下的图片滑动效果代码

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2011-4-22 01:27:26 | 显示全部楼层 |阅读模式
容器采用相对定位,图片采用绝对定位,当鼠标移动到相应的图片上,改变去left属性,用tween实现动画效果. 效果预览如下:

实现原理:
容器采用相对定位,图片采用绝对定位,当鼠标移动到相应的图片上,改变去left属性,用tween实现动画效果.

代码分析:写一个picSlider类实现代码封装
复制代码 代码如下:
<div id="container">
<img src="//files.jb51.net/file_images/article/201104/r_song1.jpg" alt="" />
<img src="//files.jb51.net/file_images/article/201104/r_song2.jpg" alt="" />
<img src="//files.jb51.net/file_images/article/201104/r_song3.jpg" alt="" />
<img src="//files.jb51.net/file_images/article/201104/r_song4.jpg" alt="" />
<img src="//files.jb51.net/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.setStyle("left",i*this.aveWidth);
}
},
start:function(){
for(var i=0;i<this.triggers.length;i++){
this.triggers.set("tween",{property:"left",duration:300, fps:80}); //为每个元素设置动画参数
this.triggers.addEvent("mouseover",this.slider.bindWithEvent(this,));//绑定slider函数
}
},
slider:function(e,at){
e.stop();
for(var i=1;i<this.triggers.length;i++){
if(i<=at){
this.triggers.get("tween").start(i*this.newAveWidth);
}else{
this.triggers.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>后面,原因不解释!
回复

使用道具 举报

8

主题

2万

回帖

52

积分

注册会员

Rank: 2

积分
52
发表于 2022-8-31 16:10:42 | 显示全部楼层
笑纳了老板
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2022-10-13 12:54:19 | 显示全部楼层
老大你好你好好你好
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

100

积分

注册会员

Rank: 2

积分
100
发表于 2022-10-23 19:30:58 | 显示全部楼层
呵呵呵呵呵呵呵a
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2022-11-20 23:33:18 | 显示全部楼层
hi哦回复iOS就看见
回复 支持 反对

使用道具 举报

5

主题

2万

回帖

183

积分

注册会员

Rank: 2

积分
183
发表于 2022-12-23 11:09:21 | 显示全部楼层
天天源码社区www.tiantianym.com
回复 支持 反对

使用道具 举报

14

主题

1万

回帖

75

积分

注册会员

Rank: 2

积分
75
发表于 2023-1-30 22:29:02 | 显示全部楼层
论坛有你更精彩!
回复 支持 反对

使用道具 举报

1

主题

2万

回帖

319

积分

中级会员

Rank: 3Rank: 3

积分
319
发表于 2023-4-17 12:58:55 | 显示全部楼层
刷屏刷屏刷屏
回复 支持 反对

使用道具 举报

1

主题

2万

回帖

59

积分

注册会员

Rank: 2

积分
59
发表于 2023-5-5 12:56:04 | 显示全部楼层
呵呵呵呵呵呵呵a
回复 支持 反对

使用道具 举报

5

主题

2万

回帖

69

积分

注册会员

Rank: 2

积分
69
发表于 2023-9-1 22:14:07 | 显示全部楼层
先把创新班才能下班才能下班
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2025-2-8 20:53 , Processed in 0.101085 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表