JavaScript
  Home arrow JavaScript arrow Page 3 - Creating a Scrolling News Panel
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Creating a Scrolling News Panel
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 65
    2004-05-26

    Table of Contents:
  • Creating a Scrolling News Panel
  • I've Got Good News, and I've Got Bad News
  • The Code
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Creating a Scrolling News Panel - The Code


    (Page 3 of 4 )

    <script language="JavaScript">
    function getObject( obj ) {
     
    var strObj
     
    if ( document.all ) {
       
    strObj = document.all.item( obj );
     
    } else if ( document.getElementById ) {
       
    strObj = document.getElementById( obj );
     
    }
      return strObj;
    }

    This is your basic and fundamental object finder. You may find it useful to 'borrow' the MM_findObj() function from Dreamweaver, but this one is short and sweet for the sake of this tutorial. Basically we're just returning the proper object reference based on which object model the browser is using. Then we'll use that in the scrolling function.

    var theTop = 20;

    var theHeight = 100;

    var theWidth = 150;

    var theLeft = 650;

    var toClip = 55;

    So before we get into the actual function, we create some global variables, and basically set them the same as the style declarations. We can tweak these as we go along to position and scroll the box precisely as we want it. We use CSS to set the initial values, but these will override them the first time the function runs. Now let's get into the function itself:

    function scrollNews( newsDiv, toMove ) {

    theDiv = getObject( newsDiv.toString() );

    if ( theDiv == null ) { return; }

    Quite simply, here we look for the layer, and if it won't be found, the function exits. So once you have all the code in place, but it doesn't work, and doesn't return any errors, this would be why. Chances are there's a typo in the news div name.

    if ( document.layers ) {

    theDiv.clip.top = toMove;

    theDiv.clip.bottom = toMove + toClip;

    theDiv.top = theTop – toMove;

    This is the code for Netscape 4. Basically we just move the div up, and the clip down. This gives the impression that the content is scrolling within the div.

    } else {

    theDiv = theDiv.style;

    theDiv.clip = "rect(" + toMove + "px " + (theWidth + theLeft) + "px " + (toMove + toClip) + "px 0px)";

    theDiv.top = theTop - toMove + 'px';

    Here we do exactly the same thing for any other browsers. However, there is an obvious difference, in that we're accessing and changing the values through the '.style' property. Also, the clipping is different, specified all in one string rather than individual values. The string should be indentical in form to the original CSS declaration, just the values will be slightly changed.

    But what happens when we get to the end of the div? Well we have to throw in a little calculation to test if it has fully scrolled past where the top initially was. When that happens, we can reset it to its original position, which will give the illusion of continuous scrolling.

    if ( ( theTop + theHeight - toMove ) < ( theTop - theHeight - 20 ) ) {

    toMove = 0;

    if ( document.layers ) {

    theDiv.clip.top = theTop;

    theDiv.clip.bottom = toClip;

    theDiv.top = theTop

    } else {

    theDiv.clip = "rect(" + toMove + "px " + (theWidth + theLeft) + "px " + (toMove + toClip) + "px 0px)";

    theDiv.top = theTop + 'px';

    }

    }

    Here we're just incrementing the move value to move another pixel, and then recursively call the function in 1/10th of a second. If the news is scrolling too fast or slow, you can adjust the speed here.

    toMove = (toMove + 1);

    setTimeout("scrollNews('" + newsDiv + "'," + toMove + ")", 100);

    }

    </script>

    And of course we need an onload caller in the body tag to get this thing moving in the first place!

    <body onload="scrollNews('news', 0);">

    More JavaScript Articles
    More By Justin Cook


       · Thank you, it worked. please explain the way to stop the list when the mouse pointer...
       · Due to the CSS property clip, that works only if the elemnt has the CSS porperty set...
     

    JAVASCRIPT ARTICLES

    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page
    - Finishing a Slide Show Application with jQue...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT