js图片左右移动的代码(html5图片向右移动代码)
JavaScript 商品展示图片左右滚动代码怎么写?
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head
meta http-equiv="Content-Type" content="text/html; charset=gb2312" /
titleJavaScript 图片滑动切换效果/title
script type="text/javascript"
var $ = function (id) {
return "string" == typeof id ? document.getElementById(id) : id;
};
var Extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
}
var CurrentStyle = function(element){
return element.currentStyle || document.defaultView.getComputedStyle(element, null);
}
var Bind = function(object, fun) {
var args = Array.prototype.slice.call(arguments).slice(2);
return function() {
return fun.apply(object, args.concat(Array.prototype.slice.call(arguments)));
}
}
var Tween = {
Quart: {
easeOut: function(t,b,c,d){
return -c * ((t=t/d-1)*t*t*t - 1) + b;
}
},
Back: {
easeOut: function(t,b,c,d,s){
if (s == undefined) s = 1.70158;
return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
}
},
Bounce: {
easeOut: function(t,b,c,d){
if ((t/=d) (1/2.75)) {
return c*(7.5625*t*t) + b;
} else if (t (2/2.75)) {
return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
} else if (t (2.5/2.75)) {
return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
} else {
return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
}
}
}
}
//容器对象,滑动对象,切换数量
var SlideTrans = function(container, slider, count, options) {
this._slider = $(slider);
this._container = $(container);//容器对象
this._timer = null;//定时器
this._count = Math.abs(count);//切换数量
this._target = 0;//目标值
this._t = this._b = this._c = 0;//tween参数
this.Index = 0;//当前索引
this.SetOptions(options);
this.Auto = !!this.options.Auto;
this.Duration = Math.abs(this.options.Duration);
this.Time = Math.abs(this.options.Time);
this.Pause = Math.abs(this.options.Pause);
this.Tween = this.options.Tween;
this.onStart = this.options.onStart;
this.onFinish = this.options.onFinish;
var bVertical = !!this.options.Vertical;
this._css = bVertical ? "top" : "left";//方向
//样式设置
var p = CurrentStyle(this._container).position;
p == "relative" || p == "absolute" || (this._container.style.position = "relative");
this._container.style.overflow = "hidden";
this._slider.style.position = "absolute";
this.Change = this.options.Change ? this.options.Change :
this._slider[bVertical ? "offsetHeight" : "offsetWidth"] / this._count;
};
SlideTrans.prototype = {
//设置默认属性
SetOptions: function(options) {
this.options = {//默认值
Vertical: true,//是否垂直方向(方向不能改)
Auto: true,//是否自动
Change: 0,//改变量
Duration: 50,//滑动持续时间
Time: 10,//滑动延时
Pause: 2000,//停顿时间(Auto为true时有效)
onStart: function(){},//开始转换时执行
onFinish: function(){},//完成转换时执行
Tween: Tween.Quart.easeOut//tween算子
};
Extend(this.options, options || {});
},
//开始切换
Run: function(index) {
//修正index
index == undefined (index = this.Index);
index 0 (index = this._count - 1) || index = this._count (index = 0);
//设置参数
this._target = -Math.abs(this.Change) * (this.Index = index);
this._t = 0;
this._b = parseInt(CurrentStyle(this._slider)[this.options.Vertical ? "top" : "left"]);
this._c = this._target - this._b;
this.onStart();
this.Move();
},
//移动
Move: function() {
clearTimeout(this._timer);
//未到达目标继续移动否则进行下一次滑动
if (this._c this._t this.Duration) {
this.MoveTo(Math.round(this.Tween(this._t++, this._b, this._c, this.Duration)));
this._timer = setTimeout(Bind(this, this.Move), this.Time);
}else{
this.MoveTo(this._target);
this.Auto (this._timer = setTimeout(Bind(this, this.Next), this.Pause));
}
},
//移动到
MoveTo: function(i) {
this._slider.style[this._css] = i + "px";
},
//下一个
Next: function() {
this.Run(++this.Index);
},
//上一个
Previous: function() {
this.Run(--this.Index);
},
//停止
Stop: function() {
clearTimeout(this._timer); this.MoveTo(this._target);
}
};
/script
/head
body
style type="text/css"
.container,.container img {
width: 280px;
height: 200px;
}
.container {
border: 1px solid #333;
}
.container img {
border: 0;
}
/style
div class="container" id="idContainer"
table id="idSlider" border="0" cellpadding="0" cellspacing="0"
tr
td
img src="image/11.jpg" /
/td
/tr
tr
td
img src="image/di.jpg" /
/td
/tr
tr
td
img src="image/head.gif" /
/td
/tr
/table
/div
br /
br /
style type="text/css"
.num {
position: absolute;
right: 5px;
bottom: 5px;
}
.num li {
float: left;
list-style: none;
color: #fff;
text-align: center;
line-height: 16px;
width: 16px;
height: 16px;
font-family: Arial;
font-size: 12px;
cursor: pointer;
margin: 1px;
border: 1px solid #707070;
background-color: #060a0b;
}
.num li.on {
line-height: 18px;
width: 18px;
height: 18px;
font-size: 14px;
border: 0;
background-color: #ce0609;
font-weight: bold;
}
/style
div class="container" id="idContainer2"
table id="idSlider2" border="0" cellpadding="0" cellspacing="0"
tr
td
img src="image/11.jpg" /
/td
td
img src="image/di.jpg" /
/td
td
img src="image/head.gif" /
/td
/tr
/table
ul class="num" id="idNum"
/ul
/div
br /
div
input id="idAuto" type="button" value="停止" /
input id="idPre" type="button" value="" /
input id="idNext" type="button" value="" /
select id="idTween"
option value="0"
默认缓动
/option
option value="1"
方式1
/option
option value="2"
方式2
/option
/select
/div
script
new SlideTrans("idContainer", "idSlider", 3).Run();
///////////////////////////////////////////////////////////
var forEach = function(array, callback, thisObject){
if(array.forEach){
array.forEach(callback, thisObject);
}else{
for (var i = 0, len = array.length; i len; i++) { callback.call(thisObject, array[i], i, array); }
}
}
var st = new SlideTrans("idContainer2", "idSlider2", 3, { Vertical: false });
var nums = [];
//插入数字
for(var i = 0, n = st._count - 1; i = n;){
(nums[i] = $("idNum").appendChild(document.createElement("li"))).innerHTML = ++i;
}
forEach(nums, function(o, i){
o.onmouseover = function(){ o.className = "on"; st.Auto = false; st.Run(i); }
o.onmouseout = function(){ o.className = ""; st.Auto = true; st.Run(); }
})
//设置按钮样式
st.onStart = function(){
forEach(nums, function(o, i){ o.className = st.Index == i ? "on" : ""; })
}
$("idAuto").onclick = function(){
if(st.Auto){
st.Auto = false; st.Stop(); this.value = "自动";
}else{
st.Auto = true; st.Run(); this.value = "停止";
}
}
$("idNext").onclick = function(){ st.Next(); }
$("idPre").onclick = function(){ st.Previous(); }
$("idTween").onchange = function(){
switch (parseInt(this.value)){
case 2 :
st.Tween = Tween.Bounce.easeOut; break;
case 1 :
st.Tween = Tween.Back.easeOut; break;
default :
st.Tween = Tween.Quart.easeOut;
}
}
st.Run();
/script
/body/html
JS控制图片向左向右移动的代码
这段代码可以: !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " " html xmlns=" " head meta http-equiv="Content-Type" content="text/html; charset=gb2312" / title图片/title script language="javascript" !-- //图片滚动列表 mengjia 070927 var Speed_1 = 10; //速度(毫秒) var Space_1 = 20; //每次移动(px) var PageWidth_1 = 116 * 3; //翻页宽度 var interval_1 = 7000; //翻页间隔 var fill_1 = 0; //整体移位 var MoveLock_1 = false; var MoveTimeObj_1; var MoveWay_1="right"; var Comp_1 = 0; var AutoPlayObj_1=null; function GetObj(objName){if(document.getElementById){return eval('document.getElementById("'+objName+'")')}else{return eval('document.all.'+objName)}} function AutoPlay_1(){clearInterval(AutoPlayObj_1);AutoPlayObj_1=setInterval('ISL_GoDown_1();ISL_StopDown_1();',interval_1)} function ISL_GoUp_1(){if(MoveLock_1)return;clearInterval(AutoPlayObj_1);MoveLock_1=true;MoveWay_1="left";MoveTimeObj_1=setInterval('ISL_ScrUp_1();',Speed_1);} function ISL_StopUp_1(){if(MoveWay_1 == "right"){return};clearInterval(MoveTimeObj_1);if((GetObj('ISL_Cont_1').scrollLeft-fill_1)%PageWidth_1!=0){Comp_1=fill_1-(GetObj('ISL_Cont_1').scrollLeft%PageWidth_1);CompScr_1()}else{MoveLock_1=false} AutoPlay_1()} function ISL_ScrUp_1(){if(GetObj('ISL_Cont_1').scrollLeft=0){GetObj('ISL_Cont_1').scrollLeft=GetObj('ISL_Cont_1').scrollLeft+GetObj('List1_1').offsetWidth} GetObj('ISL_Cont_1').scrollLeft-=Space_1} function ISL_GoDown_1(){clearInterval(MoveTimeObj_1);if(MoveLock_1)return;clearInterval(AutoPlayObj_1);MoveLock_1=true;MoveWay_1="right";ISL_ScrDown_1();MoveTimeObj_1=setInterval('ISL_ScrDown_1()',Speed_1)} function ISL_StopDown_1(){if(MoveWay_1 == "left"){return};clearInterval(MoveTimeObj_1);if(GetObj('ISL_Cont_1').scrollLeft%PageWidth_1-(fill_1=0?fill_1:fill_1+1)!=0){Comp_1=PageWidth_1-GetObj('ISL_Cont_1').scrollLeft%PageWidth_1+fill_1;CompScr_1()}else{MoveLock_1=false} AutoPlay_1()} function ISL_ScrDown_1(){if(GetObj('ISL_Cont_1').scrollLeft=GetObj('List1_1').scrollWidth){GetObj('ISL_Cont_1').scrollLeft=GetObj('ISL_Cont_1').scrollLeft-GetObj('List1_1').scrollWidth} GetObj('ISL_Cont_1').scrollLeft+=Space_1} function CompScr_1(){if(Comp_1==0){MoveLock_1=false;return} var num,TempSpeed=Speed_1,TempSpace=Space_1;if(Math.abs(Comp_1)PageWidth_1/2){TempSpace=Math.round(Math.abs(Comp_1/Space_1));if(TempSpace1){TempSpace=1}} if(Comp_10){if(Comp_1-TempSpace){Comp_1+=TempSpace;num=TempSpace}else{num=-Comp_1;Comp_1=0} GetObj('ISL_Cont_1').scrollLeft-=num;setTimeout('CompScr_1()',TempSpeed)}else{if(Comp_1TempSpace){Comp_1-=TempSpace;num=TempSpace}else{num=Comp_1;Comp_1=0} GetObj('ISL_Cont_1').scrollLeft+=num;setTimeout('CompScr_1()',TempSpeed)}} function picrun_ini(){ GetObj("List2_1").innerHTML=GetObj("List1_1").innerHTML; GetObj('ISL_Cont_1').scrollLeft=fill_1=0?fill_1:GetObj('List1_1').scrollWidth-Math.abs(fill_1); GetObj("ISL_Cont_1").onmouseover=function(){clearInterval(AutoPlayObj_1)} GetObj("ISL_Cont_1").onmouseout=function(){AutoPlay_1()} AutoPlay_1(); } //产品展示滚动图片结束 //-- /script style type="text/css" !-- BODY { BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; FONT-SIZE: 12px; BORDER-BOTTOM-WIDTH: 0px; MARGIN: 0px; FONT-FAMILY: 宋体; BACKGROUND-COLOR: #fff; BORDER-RIGHT-WIDTH: 0px } .blk_18 { BORDER-RIGHT: #e3e3e3 1px solid; BORDER-TOP: #e3e3e3 1px solid; MARGIN-TOP: 8px; FONT-SIZE: 12px; BACKGROUND: #f3f3f3; OVERFLOW: hidden; BORDER-LEFT: #e3e3e3 1px solid; WIDTH: 390px; BORDER-BOTTOM: #e3e3e3 1px solid; ZOOM: 1 } .blk_18 .pcont { FLOAT: left; OVERFLOW: hidden; WIDTH: 350px } .blk_18 .ScrCont { WIDTH: 32766px; ZOOM: 1 } .blk_18 #List1_1 { FLOAT: left } .blk_18 #List2_1 { FLOAT: left } .blk_18 .LeftBotton { BACKGROUND: url( ) no-repeat; FLOAT: left; MARGIN: 10px 1px; WIDTH: 15px; HEIGHT: 72px } .blk_18 .RightBotton { BACKGROUND: url( ) no-repeat; FLOAT: left; MARGIN: 10px 1px; WIDTH: 15px; HEIGHT: 72px } .blk_18 .LeftBotton { BACKGROUND-POSITION: 0px 0px; MARGIN-LEFT: 5px } .blk_18 .RightBotton { BACKGROUND-POSITION: 0px -100px; MARGIN-LEFT: -1px } .blk_18 .LeftBotton:hover { BACKGROUND-POSITION: -20px 0px } .blk_18 .RightBotton:hover { BACKGROUND-POSITION: -20px -100px } .blk_18 .pl IMG { DISPLAY: block; MARGIN: 6px auto 1px; CURSOR: pointer; BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BORDER-BOTTOM-STYLE: none } .blk_18 .pl { BORDER-RIGHT: #f3f3f3 1px solid; BORDER-TOP: #f3f3f3 1px solid; FLOAT: left; BORDER-LEFT: #f3f3f3 1px solid; WIDTH: 114px; LINE-HEIGHT: 24px; BORDER-BOTTOM: #f3f3f3 1px solid; TEXT-ALIGN: center; TEXT-DECORATION: underline } .blk_18 A.pl:hover { BORDER-RIGHT: #ff9900 1px solid; BORDER-TOP: #ff9900 1px solid; BACKGROUND: #fff; BORDER-LEFT: #ff9900 1px solid; COLOR: #ff9900; BORDER-BOTTOM: #ff9900 1px solid } .commu_cont3 { MARGIN: 9px 7px 7px; LINE-HEIGHT: 150% } .commu_cont3 UL { WIDTH: 188px } -- /style /head body !-- picrotate_left start -- DIV class=blk_18A onmouseup=ISL_StopUp_1() class=LeftBotton onmousedown=ISL_GoUp_1() onmouseout=ISL_StopUp_1() href="javascript:void(0);" target=_self/A DIV class=pcont id=ISL_Cont_1 DIV class=ScrCont DIV id=List1_1!-- piclist begin --A class=pl href=" " target=_blankIMG height=72 alt=20-50元夏装抢疯 src=" " width=96美女小凡最新夏装/AA class=pl href=" " target=_blankIMG height=72 alt=韩国人气小耳环 src=" " width=96韩国人气小耳环/AA class=pl href=" " target=_blankIMG height=72 alt=3万6超值装修88平 src=" " width=963万6超值装修88平/AA class=pl href=" " target=_blankIMG height=72 alt=牛干巴敲开财富门 src=" " width=96牛干巴敲开财富门/AA class=pl href=" " target=_blankIMG height=72 alt=4K至7K高性能本本 src=" " width=964K至7K高性能本本/AA class=pl href=" " target=_blankIMG height=72 alt=7万装修102平婚房 src=" " width=967万装修102平婚房/AA class=pl href=" " target=_blankIMG height=72 alt=最新小车节油为主 src=" " width=96最新小车节油为主/AA class=pl href=" " target=_blankIMG height=72 alt="热门项目 BT烤翅" src=" " width=96热门项目 BT烤翅/AA class=pl href=" " target=_blankIMG height=72 alt=流行T恤+裤装 src=" " width=96要显瘦就这么穿!/A !-- piclist end --/DIV DIV id=List2_1/DIV/DIV/DIVA onmouseup=ISL_StopDown_1() class=RightBotton onmousedown=ISL_GoDown_1() onmouseout=ISL_StopDown_1() href="javascript:void(0);" target=_self/A/DIV SCRIPT type=text/javascript !-- picrun_ini() //-- /SCRIPT !-- picrotate_left end -- /body /html
如何使用js的计时器来让一个div背景从左向右移动全部代码
你好,给你写了一个很基础的例子。参照着自己优化成你想要的效果吧。
示例是这样的,鼠标移动到div上,背景图片会从左往右移动直至最右端。鼠标移出div,背景图标从右往左直至最左端。
备注:考虑到宽度变化,本例背景图片使用百分比定位。根据你的实际情况也可改为使用像素(px)定位。
style
.bg-div?{
????height:?110px;
????background:?url()?0%?center?no-repeat?#ccc;
}
/style
div?id="J_BgDiv"?class="bg-div"/div
script
(function()?{
????var?div?=?document.getElementById('J_BgDiv');
????//?背景百分比(从左至右,0%-100%)
????var?pos?=?0;
????//?背景向右移还是向左移(0:向右,1:向左)
????var?dir?=?0;
????//?每次移动的百分比,控制速度
????var?step?=?3;
????div.addEventListener('mouseover',?function()?{
????????var?node?=?div;
????????dir?=?0;
????????if?(!div.mover)?{
????????????div.mover?=?setInterval(function()?{
????????????????//?每次移动10%
????????????????if?(dir?===?0)?{
????????????????????pos?+=?step;
????????????????}?else?{
????????????????????pos?-=?step;
????????????????}
????????????????pos?=?pos?=?100???100?:?pos;
????????????????pos?=?pos?=?0???0?:?pos;
????????????????node.style.backgroundPosition?=?pos?+?'%?center';
????????????},?20);
????????}
????},?false);
????div.addEventListener('mouseout',?function()?{
????????dir?=?1;
????},?false);
})();
/script
希望能帮你解决问题,如有疑问可以追问。