帝王谷资源网 Design By www.wdxyy.com
目前支持的是竖向与横向滚动
http://lgyweb.com/marScroll/
现在分析下无间缝实现的基本思路(竖向例子):
HTML结构:
复制代码 代码如下:
<div id="marScroll">
<ul>
<li>01</li>
<li>02</li>
<li>03</li>
<li>04</li>
<li>05</li>
</ul>
</div>
CSS:
复制代码 代码如下:
<style type="text/css">
ul,li{padding: 0;margin: 0;}
#marScroll{height: 60px;overflow: hidden;}
#marScroll li{height: 20px;line-height: 20px;font-size: 14px;}
</style>
(1)首先需要判断里面的内容高度ul结构是否高于外层div#marScrolll,则才进行无间缝滚动:
复制代码 代码如下:
// 写在匿名函数里面,防止全局变量污染
(function(){
var target = document.getElementById('marScroll'),
oUl = target.getElementsByTagName('ul')[0];
// 内容少,则直接退出此函数
if(oUl.offsetHeight<target.offsetHeight) return;
})();
(2)无间缝就是内容的无限滚动展示,那么先需要复制里面的内容,然后通过外层的scrollTop++属性,用setInterval 函数进行循环执行
复制代码 代码如下:
target.innerHTML += target.innerHTML;
/* 判断每次滚动的距离等于第一个ul的高度时,设置scrollTop为0,然后如此的循环操作就是无间滚动了
---------------------------------------------------------------------------------------------*/
// 把功能函数抽离出来,方便调用
var fn = function(){
if(target.scrollTop == oUl_h){
target.scrollTop = 0;
}else{
target.scrollTop++;
}
}
// setInterval 循环
var timer = setInterval(function(){
fn();
},30);
(3)鼠标经过此内容块时,就停止滚动
复制代码 代码如下:
// hover
target.onmouseover = function(){
clearTimeout(timer);
}
target.onmouseout = function(){
timer = setInterval(function(){
fn();
},30);
}
例子JS总代码:
复制代码 代码如下:
// 写在匿名函数里面,防止全局变量污染
(function(){
var target = document.getElementById('marScroll'),
oUl = target.getElementsByTagName('ul')[0],
oUl_h = oUl.offsetHeight;
// 内容少,则直接退出此函数
if(oUl_h<target.offsetHeight) return;
target.innerHTML += target.innerHTML;
/* 判断每次滚动的距离等于第一个ul的高度时,设置scrollTop为0,然后如此的循环操作就是无间滚动了
---------------------------------------------------------------------------------------------*/
// 把功能函数抽离出来,方便调用
var fn = function(){
if(target.scrollTop == oUl_h){
target.scrollTop = 0;
}else{
target.scrollTop++;
}
}
// setInterval 循环
var timer = setInterval(function(){
fn();
},30);
// hover
target.onmouseover = function(){
clearTimeout(timer);
}
target.onmouseout = function(){
timer = setInterval(function(){
fn();
},30);
}
})();
已经完成了个简单的竖向无间缝,为了满足更多的需求,建议封装成可以,竖向,横向,多次调用的函数。
以下是个人的封装,线上例子:
http://lgyweb.com/marScroll/
复制代码 代码如下:
function GyMarquee(opt){
this.opt = opt;
if(!document.getElementById(this.opt.targetID)) return;
this.target = document.getElementById(this.opt.targetID);
this.dir = this.opt.dir == 'crosswise'?'crosswise':'vertical';
this.effect = this.opt.effect == 'scroll'?'scroll':'marque';
this.scrollHeight = this.opt.scrollHeight;
this.init();
}
GyMarquee.prototype = {
marquee:function(){
var _that = this,
direction = 'scrollTop',
judge = this.target.scrollHeight,
timer = null;
if(this.dir == 'crosswise'){
direction = 'scrollLeft';
judge = this.itemLen*this.opt.itemWidth;
this.targetChild.style.width = this.itemLen*this.opt.itemWidth*2 + 'px';
}
var doFn = function(){
if(_that.target[direction] == judge){
_that.target[direction] = 0;
}
_that.target[direction]++;
}
timer = setInterval(function(){
doFn();
},38);
this.target.onmouseover = function(){
if(timer) clearTimeout(timer);
}
this.target.onmouseout = function(){
timer = setInterval(function(){
doFn();
},38);
}
},
scrollDo:function(){
var can = true,
_that = this;
this.target.onmouseover=function(){can=false};
this.target.onmouseout=function(){can=true};
new function (){
var stop=_that.target.scrollTop%_that.scrollHeight==0&&!can;
if(!stop)_that.target.scrollTop==parseInt(_that.target.scrollHeight/2)?_that.target.scrollTop=0:_that.target.scrollTop++;
setTimeout(arguments.callee,_that.target.scrollTop%_that.scrollHeight?20:2500);
};
},
getByClassName:function(className,parent){
var elem = [],
node = parent != undefined&&parent.nodeType==1?parent.getElementsByTagName('*'):document.getElementsByTagName('*'),
p = new RegExp("(^|\\s)"+className+"(\\s|$)");
for(var n=0,i=node.length;n<i;n++){
if(p.test(node[n].className)){
elem.push(node[n]);
}
}
return elem;
},
init:function(){
var val = 0;
if(this.dir =='crosswise'&&this.effect=='marque'&&this.opt.itemName!=''){
this.itemLen = this.target.getElementsByTagName(this.opt.itemName).length;
val = this.itemLen*this.opt.itemWidth;
}else{
val = this.target.scrollHeight;
}
var holderHTML = this.target.innerHTML;
this.target.innerHTML = '<div class="J_scrollInner">'+holderHTML+'</div>';
this.targetChild = this.getByClassName('J_scrollInner',this.target)[0];
var attr = this.dir == 'vertical'?'offsetHeight':'offsetWidth';
if(val>this.target[attr]){
if(this.effect == 'scroll'){
this.scrollDo();
}else{
this.marquee();
}
this.targetChild.innerHTML += this.targetChild.innerHTML;
}
}
}
http://lgyweb.com/marScroll/
现在分析下无间缝实现的基本思路(竖向例子):
HTML结构:
复制代码 代码如下:
<div id="marScroll">
<ul>
<li>01</li>
<li>02</li>
<li>03</li>
<li>04</li>
<li>05</li>
</ul>
</div>
CSS:
复制代码 代码如下:
<style type="text/css">
ul,li{padding: 0;margin: 0;}
#marScroll{height: 60px;overflow: hidden;}
#marScroll li{height: 20px;line-height: 20px;font-size: 14px;}
</style>
(1)首先需要判断里面的内容高度ul结构是否高于外层div#marScrolll,则才进行无间缝滚动:
复制代码 代码如下:
// 写在匿名函数里面,防止全局变量污染
(function(){
var target = document.getElementById('marScroll'),
oUl = target.getElementsByTagName('ul')[0];
// 内容少,则直接退出此函数
if(oUl.offsetHeight<target.offsetHeight) return;
})();
(2)无间缝就是内容的无限滚动展示,那么先需要复制里面的内容,然后通过外层的scrollTop++属性,用setInterval 函数进行循环执行
复制代码 代码如下:
target.innerHTML += target.innerHTML;
/* 判断每次滚动的距离等于第一个ul的高度时,设置scrollTop为0,然后如此的循环操作就是无间滚动了
---------------------------------------------------------------------------------------------*/
// 把功能函数抽离出来,方便调用
var fn = function(){
if(target.scrollTop == oUl_h){
target.scrollTop = 0;
}else{
target.scrollTop++;
}
}
// setInterval 循环
var timer = setInterval(function(){
fn();
},30);
(3)鼠标经过此内容块时,就停止滚动
复制代码 代码如下:
// hover
target.onmouseover = function(){
clearTimeout(timer);
}
target.onmouseout = function(){
timer = setInterval(function(){
fn();
},30);
}
例子JS总代码:
复制代码 代码如下:
// 写在匿名函数里面,防止全局变量污染
(function(){
var target = document.getElementById('marScroll'),
oUl = target.getElementsByTagName('ul')[0],
oUl_h = oUl.offsetHeight;
// 内容少,则直接退出此函数
if(oUl_h<target.offsetHeight) return;
target.innerHTML += target.innerHTML;
/* 判断每次滚动的距离等于第一个ul的高度时,设置scrollTop为0,然后如此的循环操作就是无间滚动了
---------------------------------------------------------------------------------------------*/
// 把功能函数抽离出来,方便调用
var fn = function(){
if(target.scrollTop == oUl_h){
target.scrollTop = 0;
}else{
target.scrollTop++;
}
}
// setInterval 循环
var timer = setInterval(function(){
fn();
},30);
// hover
target.onmouseover = function(){
clearTimeout(timer);
}
target.onmouseout = function(){
timer = setInterval(function(){
fn();
},30);
}
})();
已经完成了个简单的竖向无间缝,为了满足更多的需求,建议封装成可以,竖向,横向,多次调用的函数。
以下是个人的封装,线上例子:
http://lgyweb.com/marScroll/
复制代码 代码如下:
function GyMarquee(opt){
this.opt = opt;
if(!document.getElementById(this.opt.targetID)) return;
this.target = document.getElementById(this.opt.targetID);
this.dir = this.opt.dir == 'crosswise'?'crosswise':'vertical';
this.effect = this.opt.effect == 'scroll'?'scroll':'marque';
this.scrollHeight = this.opt.scrollHeight;
this.init();
}
GyMarquee.prototype = {
marquee:function(){
var _that = this,
direction = 'scrollTop',
judge = this.target.scrollHeight,
timer = null;
if(this.dir == 'crosswise'){
direction = 'scrollLeft';
judge = this.itemLen*this.opt.itemWidth;
this.targetChild.style.width = this.itemLen*this.opt.itemWidth*2 + 'px';
}
var doFn = function(){
if(_that.target[direction] == judge){
_that.target[direction] = 0;
}
_that.target[direction]++;
}
timer = setInterval(function(){
doFn();
},38);
this.target.onmouseover = function(){
if(timer) clearTimeout(timer);
}
this.target.onmouseout = function(){
timer = setInterval(function(){
doFn();
},38);
}
},
scrollDo:function(){
var can = true,
_that = this;
this.target.onmouseover=function(){can=false};
this.target.onmouseout=function(){can=true};
new function (){
var stop=_that.target.scrollTop%_that.scrollHeight==0&&!can;
if(!stop)_that.target.scrollTop==parseInt(_that.target.scrollHeight/2)?_that.target.scrollTop=0:_that.target.scrollTop++;
setTimeout(arguments.callee,_that.target.scrollTop%_that.scrollHeight?20:2500);
};
},
getByClassName:function(className,parent){
var elem = [],
node = parent != undefined&&parent.nodeType==1?parent.getElementsByTagName('*'):document.getElementsByTagName('*'),
p = new RegExp("(^|\\s)"+className+"(\\s|$)");
for(var n=0,i=node.length;n<i;n++){
if(p.test(node[n].className)){
elem.push(node[n]);
}
}
return elem;
},
init:function(){
var val = 0;
if(this.dir =='crosswise'&&this.effect=='marque'&&this.opt.itemName!=''){
this.itemLen = this.target.getElementsByTagName(this.opt.itemName).length;
val = this.itemLen*this.opt.itemWidth;
}else{
val = this.target.scrollHeight;
}
var holderHTML = this.target.innerHTML;
this.target.innerHTML = '<div class="J_scrollInner">'+holderHTML+'</div>';
this.targetChild = this.getByClassName('J_scrollInner',this.target)[0];
var attr = this.dir == 'vertical'?'offsetHeight':'offsetWidth';
if(val>this.target[attr]){
if(this.effect == 'scroll'){
this.scrollDo();
}else{
this.marquee();
}
this.targetChild.innerHTML += this.targetChild.innerHTML;
}
}
}
标签:
原生js,无间缝滚动
帝王谷资源网 Design By www.wdxyy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
帝王谷资源网 Design By www.wdxyy.com
暂无评论...
更新日志
2024年10月25日
2024年10月25日
- 群星2013-青春缤纷辑压箱宝大公开3CD2[新加坡限量版][WAV整轨]
- 林育群.2013-BalladShow(日本版)【环球】【WAV+CUE】
- 陈加洛.1992-痛到感觉不到【宝丽金】【WAV+CUE】
- 群星.2023-宿命之敌电视剧原声带【韶愔音乐】【FLAC分轨】
- 東京事変-大発見[FLAC+CUE]
- 椎名林檎-三文ゴシップ[FLAC+CUE]
- 2024年08月04日
- 裘德《裘德「最后的水族馆」演唱会LIVE》[320K/MP3][228.89MB]
- 裘德《裘德「最后的水族馆」演唱会LIVE》[24bit 48kHz][FLAC/分轨][2.08G]
- 基因三重奏《如果你什么都不说 音乐会现场录音》[320K/MP3][145.37MB]
- 孟庭苇.1996-月亮说话(2020环球24KGOLD限量版)【上华】【WAV+CUE】
- 群星.1997-新艺宝优质音响系列·国语精选监听版【新艺宝】【WAV+CUE】
- 阿桑.2005-寂寞在唱歌(星外星引进版)【华研国际】【WAV+CUE】
- 基因三重奏《如果你什么都不说 音乐会现场录音》[FLAC/分轨][287.43MB]
- 蔡题谦《我爱你,却依然要看你走》[320K/MP3][88.65MB]