javascript文字水平滾動程式碼例項

antzone發表於2017-04-12

本本章節分享一段程式碼例項,它實現了文字水平滾動效果。

需要的朋友可以做一下參考,程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style type="text/css">
.pad_right{ padding-right:2em;}
#scroll_div {
  height:26px;
  overflow: hidden;
  white-space: nowrap;
  width:535px;
  margin-left:10px;
}
#scroll_begin,#scroll_end {display: inline;}
</style>
<script type="text/javascript">
function ScrollImgLeft() {
  var speed = 50;
  var MyMar = null;
  var scroll_begin = document.getElementById("scroll_begin");
  var scroll_end = document.getElementById("scroll_end");
  var scroll_div = document.getElementById("scroll_div");
  scroll_end.innerHTML = scroll_begin.innerHTML;
  function Marquee() {
    if (scroll_end.offsetWidth - scroll_div.scrollLeft <= 0) {
      scroll_div.scrollLeft -= scroll_begin.offsetWidth;
    }
    else {
      scroll_div.scrollLeft++;
    }    
  }
  MyMar = setInterval(Marquee, speed);
  scroll_div.onmouseover = function () {
    clearInterval(MyMar);
  }
  scroll_div.onmouseout = function () {
    MyMar = setInterval(Marquee, speed);
  }
}
window.onload = function () {
  ScrollImgLeft();
}
</script>
</head>
<body>
<div id="scroll_div" class="fl">
  <div id="scroll_begin">
    螞蟻部落提醒一 <span class="pad_right">只有努力奮鬥才會有美好未來</span>
    螞蟻部落提醒二 <span class="pad_right">沒有人一開始就是高手</span>
    螞蟻部落提醒三 <span class="pad_right">每一天都是新的必須要好好珍惜</span>
    螞蟻部落提醒四 <span class="pad_right">分享和互助式進步的推動者</span>
    螞蟻部落提醒五 <span class="pad_right">珍惜當下,因為其他的都是虛幻</span>
    螞蟻部落提醒六 <span class="pad_right">認真的對待每一件事情</span>
    螞蟻部落提醒七 <span class="pad_right">保重自己的身體</span>
  </div>
  <div id="scroll_end"></div>
</div> 
</body>
</html>

相關文章