常见JS效果之图片减速度滚动实现代码
程序员文章站
2023-11-24 14:41:40
复制代码 代码如下:
<!doctype html>
<html>
<head>
<meta charset="gb2312" />
<title>图片减速度滚动(by rentj1@163.com;)</title>
<style>
ul{ margin:0; padding:0}
ul{ list-style:none;}
body{font:12px/1.2 "宋体"; }
.scroll_box{ position:relative; height:164px; width:766px; border:1px solid #efefef; overflow:hidden;}
.scroll_box .list{ overflow:hidden; zoom:1; position:absolute;}
.scroll_box .list li{ width:235px; height:164px; padding:0 10px; text-align:center; float:left;}
.scroll_box .list .info{ line-height:1.5}
</style>
</head>
<body>
<div class="scroll_box" id="container">
<ul class="list" id="content">
<li>
<img src="http://img14.360buyimg.com/n4/4587/cd8a8a76-24f4-44d8-876b-12d78cbae588.jpg" />
<div class="info">
兵尼兔 环保大包围专车专用脚垫 米色(专车专用,仅支持在线支付)版型精准 大包围
</div>
<span class="price">会员价:¥288.00</span>
</li>
<li>
<img src="http://img11.360buyimg.com/n4/309/9230eaef-021c-447d-a64b-51ce698822ef.jpg" />
<div class="info">
兵尼兔 环保大包围专车专用脚垫 米色(专车专用,仅支持在线支付)版型精准 大包围
</div>
<span class="price">会员价:¥288.00</span>
</li>
<li>
<img src="http://img10.360buyimg.com/n4/8792/7691b514-a0b3-469e-8a2b-37b219ebc490.jpg" />
<div class="info">
兵尼兔 环保大包围专车专用脚垫 米色(专车专用,仅支持在线支付)版型精准 大包围
</div>
<span class="price">会员价:¥288.00</span>
</li>
<li>
<img src="http://img12.360buyimg.com/n4/285/c675cbb0-3a6f-4703-95fb-e75a7c90b48e.jpg" />
<div class="info">
兵尼兔 环保大包围专车专用脚垫 米色(专车专用,仅支持在线支付)版型精准 大包围
</div>
<span class="price">会员价:¥288.00</span>
</li>
<li>
<img src="http://img12.360buyimg.com/n4/1957/22aa7c6d-4b70-49b8-a985-678b1d0e3781.jpg" />
<div class="info">
兵尼兔 环保大包围专车专用脚垫 米色(专车专用,仅支持在线支付)版型精准 大包围
</div>
<span class="price">会员价:¥288.00</span>
</li>
<li>
<img src="http://img10.360buyimg.com/n4/2314/cf09b35a-eb6d-4a4f-a4dd-3301bfafef99.jpg" />
<div class="info">
兵尼兔 环保大包围专车专用脚垫 米色(专车专用,仅支持在线支付)版型精准 大包围
</div>
<span class="price">会员价:¥288.00</span>
</li>
</ul>
</div>
<script>
untils = {
getcomputedstyle: function(elem,syle){
var computedstyle = document.defaultview
&& document.defaultview.getcomputedstyle
&& document.defaultview.getcomputedstyle(elem,null);
if(!computedstyle){
computedstyle = elem.currentstyle
}
return computedstyle[syle]
},
setstyle: function(elem, name,val){
elem.style[name] = val;
},
get: function(id){
document.getelementbyid(id)
}
}
function scrollslider(container, content){
var clone = content.clonenode(true);
var initcss= "left:0; top:0";
var contentheight = content.offsetheight;
var containerheight = container.clientheight;
clone.id = "content-clone";
container.appendchild(clone);
clone.setattribute("csstext", initcss);
content.setattribute("csstext", initcss);
clone.style.margintop = contentheight+"px";
this.content = content;
this.clone = clone;
this.container = container;
this.containerheight = containerheight;
this.contentheight = contentheight;
}
scrollslider.prototype = {
start: function(delay){
var _this = this;
clearinterval(_this.timer);
_this.timer = setinterval(function(){
_this.scroll();
},delay);
},
scroll: function(){
var margintop = parseint(untils.getcomputedstyle(this.content, "margintop"));
var offsettop = this.contentheight;
if(isnan(margintop))return;
if( -margintop >= this.contentheight){
margintop = this.contentheight;
}
if(margintop > 0){
offsettop = -offsettop;
}
this.move(margintop, offsettop);
},
move: function(margintop, offsettop){
var s = this.containerheight, s1 = 0, _this = this, m = 0;
var timer = setinterval(function(){
var speed = (s-s1)/8;
speed = speed > 0 ? math.ceil(speed) : math.floor(speed);
s1 += speed;
m = margintop - s1;
untils.setstyle(_this.content, "margintop", m + "px");
untils.setstyle(_this.clone, "margintop", (m + offsettop)+"px");
if(s-s1 <= 0){
clearinterval(timer);
}
},30);
}
};
var container = document.getelementbyid("container")
var content = document.getelementbyid("content")
var xx = new scrollslider(container, content);
xx.start(1000*3);
/*
//s=1/2 * at^2
//s (att)/2
//0<s<164
0,-164,-328,164,0
328,164,0,-164,-328,164,0
*/
</script>
</body>
</html>
复制代码 代码如下:
<!doctype html>
<html>
<head>
<meta charset="gb2312" />
<title>图片减速度滚动(by rentj1@163.com;)</title>
<style>
ul{ margin:0; padding:0}
ul{ list-style:none;}
body{font:12px/1.2 "宋体"; }
.scroll_box{ position:relative; height:164px; width:766px; border:1px solid #efefef; overflow:hidden;}
.scroll_box .list{ overflow:hidden; zoom:1; position:absolute;}
.scroll_box .list li{ width:235px; height:164px; padding:0 10px; text-align:center; float:left;}
.scroll_box .list .info{ line-height:1.5}
</style>
</head>
<body>
<div class="scroll_box" id="container">
<ul class="list" id="content">
<li>
<img src="http://img14.360buyimg.com/n4/4587/cd8a8a76-24f4-44d8-876b-12d78cbae588.jpg" />
<div class="info">
兵尼兔 环保大包围专车专用脚垫 米色(专车专用,仅支持在线支付)版型精准 大包围
</div>
<span class="price">会员价:¥288.00</span>
</li>
<li>
<img src="http://img11.360buyimg.com/n4/309/9230eaef-021c-447d-a64b-51ce698822ef.jpg" />
<div class="info">
兵尼兔 环保大包围专车专用脚垫 米色(专车专用,仅支持在线支付)版型精准 大包围
</div>
<span class="price">会员价:¥288.00</span>
</li>
<li>
<img src="http://img10.360buyimg.com/n4/8792/7691b514-a0b3-469e-8a2b-37b219ebc490.jpg" />
<div class="info">
兵尼兔 环保大包围专车专用脚垫 米色(专车专用,仅支持在线支付)版型精准 大包围
</div>
<span class="price">会员价:¥288.00</span>
</li>
<li>
<img src="http://img12.360buyimg.com/n4/285/c675cbb0-3a6f-4703-95fb-e75a7c90b48e.jpg" />
<div class="info">
兵尼兔 环保大包围专车专用脚垫 米色(专车专用,仅支持在线支付)版型精准 大包围
</div>
<span class="price">会员价:¥288.00</span>
</li>
<li>
<img src="http://img12.360buyimg.com/n4/1957/22aa7c6d-4b70-49b8-a985-678b1d0e3781.jpg" />
<div class="info">
兵尼兔 环保大包围专车专用脚垫 米色(专车专用,仅支持在线支付)版型精准 大包围
</div>
<span class="price">会员价:¥288.00</span>
</li>
<li>
<img src="http://img10.360buyimg.com/n4/2314/cf09b35a-eb6d-4a4f-a4dd-3301bfafef99.jpg" />
<div class="info">
兵尼兔 环保大包围专车专用脚垫 米色(专车专用,仅支持在线支付)版型精准 大包围
</div>
<span class="price">会员价:¥288.00</span>
</li>
</ul>
</div>
<script>
untils = {
getcomputedstyle: function(elem,syle){
var computedstyle = document.defaultview
&& document.defaultview.getcomputedstyle
&& document.defaultview.getcomputedstyle(elem,null);
if(!computedstyle){
computedstyle = elem.currentstyle
}
return computedstyle[syle]
},
setstyle: function(elem, name,val){
elem.style[name] = val;
},
get: function(id){
document.getelementbyid(id)
}
}
function scrollslider(container, content){
var clone = content.clonenode(true);
var initcss= "left:0; top:0";
var contentheight = content.offsetheight;
var containerheight = container.clientheight;
clone.id = "content-clone";
container.appendchild(clone);
clone.setattribute("csstext", initcss);
content.setattribute("csstext", initcss);
clone.style.margintop = contentheight+"px";
this.content = content;
this.clone = clone;
this.container = container;
this.containerheight = containerheight;
this.contentheight = contentheight;
}
scrollslider.prototype = {
start: function(delay){
var _this = this;
clearinterval(_this.timer);
_this.timer = setinterval(function(){
_this.scroll();
},delay);
},
scroll: function(){
var margintop = parseint(untils.getcomputedstyle(this.content, "margintop"));
var offsettop = this.contentheight;
if(isnan(margintop))return;
if( -margintop >= this.contentheight){
margintop = this.contentheight;
}
if(margintop > 0){
offsettop = -offsettop;
}
this.move(margintop, offsettop);
},
move: function(margintop, offsettop){
var s = this.containerheight, s1 = 0, _this = this, m = 0;
var timer = setinterval(function(){
var speed = (s-s1)/8;
speed = speed > 0 ? math.ceil(speed) : math.floor(speed);
s1 += speed;
m = margintop - s1;
untils.setstyle(_this.content, "margintop", m + "px");
untils.setstyle(_this.clone, "margintop", (m + offsettop)+"px");
if(s-s1 <= 0){
clearinterval(timer);
}
},30);
}
};
var container = document.getelementbyid("container")
var content = document.getelementbyid("content")
var xx = new scrollslider(container, content);
xx.start(1000*3);
/*
//s=1/2 * at^2
//s (att)/2
//0<s<164
0,-164,-328,164,0
328,164,0,-164,-328,164,0
*/
</script>
</body>
</html>