以更少的代码实现相同的效果应该是我们写程序的究极目标,当然可读性不能丢。在基于javascript的无缝滚动动画实现1最后一个运行框,我已经做了这样的探索。不过换汤不换药,还是来来回回在scrollTop/scrollLeft与offsetTop/offsetLeft上做文章。总的思路基本是这样,让某个元素整体向某个方向移动,这样它里面的内容(图片或文字)就跟着移动,当元素移动到某一个距离后就回到原点。为了防止内容移着移着就没有了,我们需要两套相同的内容。在第一部分,第二套内容是动态生成的,并复制到另一个兄弟元素中,这在水平滚动时,我们需要花一些周折来让所有内容水平排列在一起。我就想,如果把第二套内容直接加在相同的元素是否效果会更好呢?起码CSS也少写一点。用scrollTop/scrollLeft实现滚动,还有一个缺点,它对父容器的宽度与高度有严格的规定的。比如我们想上下滚动图片,ul作为容器,li的图片为滚动内容,那么ul的高至少为图片的高的两倍(我们假设每张图片的尺寸都是相等的),否则就动不了。鉴此,我开发了新一代的无缝滚动函数。
<ul id="marquee"> <li> <a href="http://www.cnblogs.com/rubylouvre/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/UploadFiles/2021-04-02/o_s017.jpg">#marquee ,#marquee li { padding:0; margin:0; } #marquee { position:relative; list-style:none; height:150px; width:200px; overflow:hidden; border:10px solid #369; } #marquee li { position:absolute; } #marquee img { display:block; border:0; }var Marquee = function(id){ try{document.execCommand("BackgroundImageCache", false, true);}catch(e){}; var container = document.getElementById(id), slide = container.getElementsByTagName("li")[0], speed = arguments[1] || 10, delta = 0,//当前滚动的位置 critical = slide.offsetHeight;//临界值 slide.innerHTML += slide.innerHTML; var rolling = function(){ delta == -critical "px"; } var timer = setInterval(rolling,speed)//设置定时器 container.onmouseover=function() {clearInterval(timer)}//鼠标移到marquee上时,清除定时器,停止滚动 container.onmouseout=function() {timer=setInterval(rolling,speed)}//鼠标移开时重设定时器 } window.onload = function(){ Marquee("marquee"); }<!doctype html> <title>javascript无缝滚动 by 司徒正美</title> <meta charset="utf-8"/> <meta name="keywords" content="javascript无缝滚动 by 司徒正美" /> <meta name="description" content="javascript无缝滚动 by 司徒正美" /> <base href="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <style type="text/css"> h1 { font:400 16px/1 "Microsoft YaHei",KaiTi_GB2312,SimSun } #marquee ,#marquee li { padding:0; margin:0; } #marquee { position:relative; list-style:none; height:150px; width:200px; overflow:hidden; border:10px solid #369; } #marquee li { position:absolute; } #marquee img { display:block; border:0; } </style> <script type="text/javascript"> var Marquee = function(id){ try{document.execCommand("BackgroundImageCache", false, true);}catch(e){}; var container = document.getElementById(id), slide = container.getElementsByTagName("li")[0], speed = arguments[1] || 10, delta = 0,//当前滚动的位置 critical = slide.offsetHeight;//临界点 slide.innerHTML += slide.innerHTML; var rolling = function(){ delta == -critical "px"; } var timer = setInterval(rolling,speed)//设置定时器 container.onmouseover=function() {clearInterval(timer)}//鼠标移到marquee上时,清除定时器,停止滚动 container.onmouseout=function() {timer=setInterval(rolling,speed)}//鼠标移开时重设定时器 } window.onload = function(){ Marquee("marquee"); } </script> <h1>javascript无缝滚动(向上滚动) by 司徒正美</h1> <base href="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <ul id="marquee"> <li> <a href="http://www.cnblogs.com/rubylouvre/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/UploadFiles/2021-04-02/o_s017.jpg">向下滚动也很容易,只不过一开始把它定位到第一套内容与第二套内容之间,也就是slide.offsetHeight,不过我们需要把它改成负数让向整体向下移动,然后再慢慢向上移动。
<!doctype html> <title>javascript无缝滚动 by 司徒正美</title> <meta charset="utf-8"/> <meta name="keywords" content="javascript无缝滚动 by 司徒正美" /> <meta name="description" content="javascript无缝滚动 by 司徒正美" /> <base href="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <style type="text/css"> h1 { font:400 16px/1 "Microsoft YaHei",KaiTi_GB2312,SimSun } #marquee ,#marquee li { padding:0; margin:0; } #marquee { position:relative; list-style:none; height:150px; width:200px; overflow:hidden; border:10px solid #B45B3E; } #marquee li { position:absolute; } #marquee img { display:block; border:0; } </style> <script type="text/javascript"> var Marquee = function(id){ try{document.execCommand("BackgroundImageCache", false, true);}catch(e){}; var container = document.getElementById(id), slide = container.getElementsByTagName("li")[0], speed = arguments[1] || 10, critical = slide.offsetHeight,//临界值 delta = -critical; slide.innerHTML += slide.innerHTML; var rolling = function(){ delta == 0 "px"; } var timer = setInterval(rolling,speed)//设置定时器 container.onmouseover=function() {clearInterval(timer)}//鼠标移到marquee上时,清除定时器,停止滚动 container.onmouseout=function() {timer=setInterval(rolling,speed)}//鼠标移开时重设定时器 } window.onload = function(){ Marquee("marquee"); } </script> <h1>javascript无缝滚动(向下滚动) by 司徒正美</h1> <base href="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <ul id="marquee"> <li> <a href="http://www.cnblogs.com/rubylouvre/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/UploadFiles/2021-04-02/o_s017.jpg">实现水平移动也比第一部分容易,结构层完全不用改动,表现层也改动很少。
#marquee ,#marquee li { padding:0; margin:0; } #marquee { position:relative; list-style:none; height:150px; width:200px; overflow:hidden; border:10px solid #B45B3E; } #marquee li { position:absolute; width:1000%;/*★★新增加★★*/ } #marquee a { float:left;/*★★新增加★★*/ } #marquee img { display:block; border:0; }难点是如何获取那个临界值,也就是夹在第一套内容与第二个套内容之间的那个位置。由于第二套内容我们并没有外套一个元素,因此我们需要从滚动对象下手。滚动对象都是a元素,我们需要计算出第二套内容的第一个a元素到达父元素左边的距离。这个值就是我们要求的临界值。明白这点,就轻松了。
//*************略********* slide.innerHTML += slide.innerHTML; var item = slide.getElementsByTagName("a"), critical = item[item.length/2].offsetLeft,//临界值 //*************略*********<!doctype html> <title>javascript无缝滚动 by 司徒正美</title> <meta charset="utf-8"/> <meta name="keywords" content="javascript无缝滚动 by 司徒正美" /> <meta name="description" content="javascript无缝滚动 by 司徒正美" /> <base href="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <style type="text/css"> h1 { font:400 16px/1 "Microsoft YaHei",KaiTi_GB2312,SimSun } #marquee ,#marquee li { padding:0; margin:0; } #marquee { position:relative; list-style:none; height:150px; width:200px; overflow:hidden; border:10px solid #8080C0; } #marquee li { position:absolute; width:1000%;/*新增加*/ } #marquee a { float:left;/*新增加*/ } #marquee img { display:block; border:0; } </style> <script type="text/javascript"> var Marquee = function(id){ try{document.execCommand("BackgroundImageCache", false, true);}catch(e){}; var container = document.getElementById(id), slide = container.getElementsByTagName("li")[0], speed = arguments[1] || 10; slide.innerHTML += slide.innerHTML; var item = slide.getElementsByTagName("a"), critical = item[item.length/2].offsetLeft,//临界值 delta = 0; var rolling = function(){ delta == -critical "px"; } var timer = setInterval(rolling,speed)//设置定时器 container.onmouseover=function() {clearInterval(timer)}//鼠标移到marquee上时,清除定时器,停止滚动 container.onmouseout=function() {timer=setInterval(rolling,speed)}//鼠标移开时重设定时器 } window.onload = function(){ Marquee("marquee"); } </script> <h1>javascript无缝滚动(向左滚动) by 司徒正美</h1> <base href="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <ul id="marquee"> <li> <a href="http://www.cnblogs.com/rubylouvre/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/UploadFiles/2021-04-02/o_s017.jpg">下面是向右滚动,由于都是不足20行的脚本,应该一看源码就明白,不详述了。
<!doctype html> <title>javascript无缝滚动 by 司徒正美</title> <meta charset="utf-8"/> <meta name="keywords" content="javascript无缝滚动 by 司徒正美" /> <meta name="description" content="javascript无缝滚动 by 司徒正美" /> <base href="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <style type="text/css"> h1 { font:400 16px/1 "Microsoft YaHei",KaiTi_GB2312,SimSun } #marquee ,#marquee li { padding:0; margin:0; } #marquee { position:relative; list-style:none; height:150px; width:200px; overflow:hidden; border:10px solid #6cc; } #marquee li { position:absolute; width:1000%; } #marquee a { float:left; } #marquee img { display:block; border:0; } </style> <script type="text/javascript"> var Marquee = function(id){ try{document.execCommand("BackgroundImageCache", false, true);}catch(e){}; var container = document.getElementById(id), slide = container.getElementsByTagName("li")[0], speed = arguments[1] || 10; slide.innerHTML += slide.innerHTML; var item = slide.getElementsByTagName("a"),//临界点 critical = item[item.length/2].offsetLeft, delta = -critical; var rolling = function(){ delta == 0 "px"; } var timer = setInterval(rolling,speed)//设置定时器 container.onmouseover=function() {clearInterval(timer)}//鼠标移到marquee上时,清除定时器,停止滚动 container.onmouseout=function() {timer=setInterval(rolling,speed)}//鼠标移开时重设定时器 } window.onload = function(){ Marquee("marquee"); } </script> <h1>javascript无缝滚动(向右滚动) by 司徒正美</h1> <base href="http://images.cnblogs.com/cnblogs_com/rubylouvre/199042/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <ul id="marquee"> <li> <a href="http://www.cnblogs.com/rubylouvre/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/UploadFiles/2021-04-02/o_s017.jpg">我们可以把上面四个函数合并成一个函数,结构层与表现层参照向上滚动。
var Marquee = function(id){ try{document.execCommand("BackgroundImageCache", false, true);}catch(e){}; var container = document.getElementById(id), slide = container.getElementsByTagName("li")[0], options = arguments[1] || {}, speed = options.speed || 10, direction = options.direction || "left"; slide.innerHTML += slide.innerHTML; var item = slide.getElementsByTagName("a"), critical,delta,rolling,field; if(direction == "left" || direction == "right"){ slide.style.cssText = "width:1000%"; for(var i=0 ,l=item.length; i<l;i++){ item[i].style.cssText = "float:left"; } field = "left"; critical = item[item.length/2].offsetLeft; }else if(direction == "up" || direction == "down"){ field = "top"; critical = item[item.length/2].offsetTop; } if(direction == "up" || direction == "left"){ delta = 0; rolling = function(){ delta == -critical "px"; } }else if(direction == "down" || direction == "right"){ delta = -critical; rolling = function(){ delta == 0 "px"; } } var timer = setInterval(rolling,speed)//设置定时器 container.onmouseover=function() {clearInterval(timer)}//鼠标移到marquee上时,清除定时器,停止滚动 container.onmouseout=function() {timer=setInterval(rolling,speed)}//鼠标移开时重设定时器 }使用:
Marquee("marquee",{speed:35,direction:"up"});有了这个我们实现另一种图片轮换就容易了,这里先做个预告吧。本文到此为止。
华山资源网 Design By www.eoogi.com广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!华山资源网 Design By www.eoogi.com暂无评论...
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]