search
HomeWeb Front-endJS TutorialJavaScript method to implement rolling news_javascript skills

The example in this article describes the method of implementing rolling news in javaScript. Share it with everyone for your reference. The details are as follows:

The rolling_new.html page is as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
 <title> New Document </title>
 <meta name="Generator" content="EditPlus">
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <meta name="Author" content="">
 <meta name="Keywords" content="">
 <meta name="Description" content="">
 </head>
 <style type="text/css">
 *{margin:0;padding:0;}
 #news{display:none;}
 </style>
 <body>
 <div id="news">
 太平天国医疗卫生组织,在国家制度上,有一定的组织,已经形成为一
种正规化的制度。它可分为朝内、军中、居民三个系统。
种正规化的制度。它可分为朝内、军中、居民三个系统。
种正规化的制度。它可分为朝内、军中、居民三个系统。
种正规化的制度。它可分为朝内、军中、居民三个系统。种正规化的制度。它可分为朝内、军中、居民三个系统。种正规化的制度。它可分为朝内、军中、居民三个系统。种正规化的制度。它可分为朝内、军中、居民三个系统。种正规化的制度。它可分为朝内、军中、居民三个系统。种正规化的制度。它可分为朝内、军中、居民三个系统。种正规化的制度。它可分为朝内、军中、居民三个系统。
 </div>
 <div id="show_news">
 </div>
 </body>
 <script type="text/javascript">
 function $(node){
  return document.getElementById(node);
 }
 function getElementsByClassName(str,root,tag){
   if(root){
    root=typeof root=="string"&#63;document.getElementById(root):root;
   }else{
    root=document.body;
   }
   tag=tag||"*";
   var els=root.getElementsByTagName(tag),arr=[];
   for(var i=0,n=els.length;i<n;i++){
    for(var j=0,k=els[i].className.split(" "),l=k.length;j<1;j++){
     if(k[j]==str){
      arr.push(els[i]);
      break;
     }
    }
   }
   return arr;
 }
 function attachEvent(node,eventType,handler){
  node=typeof node=="string"&#63;document.getElementById(node):node;
  if(document.all){
   node.attachEvent("on"+eventType,handler);
  }else{
   node.addEventListener(eventType,handler,false);
  }
 }
 function rolling_news(source,target,width,height,speed,direction){
  this.source=$(source);
  this.source_content=$(source).innerHTML;
  this.target=$(target);
  this.width=width;  //宽
  this.height=height;  //高
  this.speed=speed;  //滚动速度
  this.direction=direction;//方向
  this.tag=0;  
 }
 rolling_news.prototype={
  version:"1.00",
  author:"yangfeifei",
  date:"2011-10-23",
  initialize:function(){
   var o=this;
   var target=o.target;
   var content=o.source_content;
   var innerDiv=document.createElement("div");
   innerDiv.setAttribute("class","innerDiv");
   o.source.innerHTML="";
   innerDiv.innerHTML=o.source_content;
   target.appendChild(innerDiv);  
   //显示区域样式
   target.style.width=o.width+"px";
   target.style.height=o.height+"px";
   target.style.overflow="hidden";
   target.getElementsByTagName('div')[0].style.width=o.width+"px";
   target.getElementsByTagName('div')[0].style.height=target.getElementsByTagName('div')[0].scrollHeight>o.height&#63;target.getElementsByTagName('div')[0].scrollHeight+"px":o.height+"px";//当文档实际高度大于容器时,高度为实际文档高度,否则为容器高度
   //显示区域初始化
   switch(o.direction){
    case "up":
    target.scrollTop="0";
    o.addAfterNode();
    break;
    case "down":
    o.addBeforeNode();
    target.scrollTop=target.scrollHeight-o.height;
    break;
   }   
   //初始动作    
   o.autoplay();
   attachEvent(o.target,'mouseover',function(){o.stop()});
   attachEvent(o.target,'mouseout',function(){o.autoplay()});
  },
  up:function(){
   var x=this;
   var divHeight=x.target.getElementsByTagName('div')[0].scrollHeight>x.height&#63;x.target.getElementsByTagName('div')[0].scrollHeight:x.height;
   if((x.target.scrollHeight-x.target.scrollTop)!=x.height){
     x.target.scrollTop=x.tag;
   }else{
    x.addAfterNode();
    x.target.removeChild(x.target.getElementsByTagName('div')[0]);    
    x.tag=x.tag-divHeight;
    x.target.scrollTop=x.tag;     
   }
   x.tag=x.tag+x.speed;
  },
  down:function(){
   var j=this;
   var divHeight=j.target.getElementsByTagName('div')[0].scrollHeight>j.height&#63;j.target.getElementsByTagName('div')[0].scrollHeight:j.height;
   if(j.target.scrollTop==0){
    j.addBeforeNode();
    j.target.removeChild(j.target.getElementsByTagName('div')[2]); 
    j.tag=j.tag-divHeight;
    j.target.scrollTop=j.target.scrollHeight-j.height-j.tag;
   }else{
    j.target.scrollTop=j.target.scrollHeight-j.height-j.tag;
   }
   j.tag=j.tag+j.speed;
  },
  addAfterNode:function(){
   var p=this;
   var newDiv=document.createElement('div');
   newDiv.setAttribute("class","innerDiv");
   newDiv.innerHTML=p.source_content;
   p.target.appendChild(newDiv);
   newDiv.style.width=p.width+"px";
   newDiv.style.height=p.target.getElementsByTagName('div')[0].scrollHeight>p.height&#63;p.target.getElementsByTagName('div')[0].scrollHeight+"px":p.height+"px";//当文档实际高度大于容器时,高度为实际文档高度,否则为容器高度
  },
  addBeforeNode:function(){
   var d=this;
   var newDiv=document.createElement('div');
   newDiv.setAttribute("class","innerDiv");
   newDiv.innerHTML=d.source_content;
   d.target.insertBefore(newDiv,d.target.getElementsByTagName('div')[0]);
   newDiv.style.width=d.width+"px";
   newDiv.style.height=d.target.getElementsByTagName('div')[0].scrollHeight>d.height&#63;d.target.getElementsByTagName('div')[0].scrollHeight+"px":d.height+"px";//当文档实际高度大于容器时,高度为实际文档高度,否则为容器高度
  },
  play:function(){
   var t=this;     
   switch(t.direction){
    //向上
    case "up":
     t.up();
     break;
    //向右
    case "down": 
     t.down();
     break;     
   } 
  },
  autoplay:function(){
   var s=this;
   s.auto=setInterval(function(){s.play()},1);
  },
  stop:function(){
   var h=this;
   clearInterval(h.auto);
  }
 }
 var a=new rolling_news("news","show_news",200,200,1,"down");
 a.initialize();
 </script>
</html>

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool