js实现marquee类似效果

css:

#timerSliding {
    overflow: hidden;
    width: 320px;
    height: 430px;
    position: relative;
    margin-left: 30px;
    vertical-align: top;
}

 

#timerSliding_content {
 font-size: 12px;
 color: #f0f0f0;
 width: 320px;
 word-wrap: break-word;
 overflow: hidden;
 position: absolute;
 vertical-align: top;
}

 

 

html:

<div id='timerSliding'>
     <div id="timerSliding_content" style="top: 0px;" οnmοusemοve="setStopped();" οnmοuseοut="setStart();">
                     输入滚动的文字。

     </div>
</div>

 

 

js:

var tm_intervalTime = 50;
var tm_speed = 10;
var tm_intervalId;
var move_span = 0;
var stopped = false;
var contentHeight = 0;
var times = 0;
var curHeight = 0;
var curDiv = 0;
       
function Page_Onload() {
 var content = document.getElementById('timerSliding_content');
 // var scontent = document.getElementById('timerSliding_seconde_content');
 //content.innerHTML = content.innerHTML + content.innerHTML;
 if(content) {
  contentHeight = parseInt(content.clientHeight);
  times = contentHeight / 430;
  if(times <= 1) {
   return;
  }
  content.style.position = 'absolute';
  content.style.top = '0px';
  curHeight = 0;
  curDiv = 0;
  stopped = false;
  window.setInterval(AfterBraek, 8000);
 }
}

function AfterBraek() {
 if(stopped)
  return;

 if(tm_intervalId) {
  return;
 }
 var content = document.getElementById('timerSliding_content');
 if(content) {
  var top = parseInt(content.style.top);
  if (top < (430 - contentHeight)) {
   content.style.top = '430';
   curDiv = 0;
   curHeight = 430;
  } else {
   var divs = content.getElementsByTagName("div");
   curHeight = 0;
   if(divs && divs.length > 0) {
    if(divs.length <= curDiv) {
     curDiv = 0;
    }
    while(curDiv < divs.length) {
     curHeight += divs[curDiv].offsetHeight;
     if(curHeight > 430) {
      curHeight -= divs[curDiv].offsetHeight;
      break;
     }
     curDiv++;
    }
   }
   //alert("curHeight:" + curHeight);
  }
  
  move_span = 0;
  tm_intervalId = window.setInterval(TM_Sliding_OnTime, tm_intervalTime);
 }
}

function TM_Sliding_OnTime() {
 if(stopped)
  return;
 var content = document.getElementById('timerSliding_content');
 var top = parseInt(content.style.top);
 if((move_span + tm_speed) > curHeight) {
  top = top - curHeight + move_span;
  move_span = curHeight;
 } else {
  top = top - tm_speed;
  move_span += tm_speed;
 }
 content.style.top = top + 'px';

 if (move_span >= curHeight) {
  window.clearInterval(tm_intervalId);
  move_span = 0;
  tm_intervalId = null;
 }
}

function setStopped() {
 if(!stopped)
  stopped = true;
}

function setStart() {
 if(stopped)
  stopped = false;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值