DDR爱好者之家 Design By 杰米

以更少的代码实现相同的效果应该是我们写程序的究极目标,当然可读性不能丢。在基于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"});

有了这个我们实现另一种图片轮换就容易了,这里先做个预告吧。本文到此为止。

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。