473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Rotating News Script

73
Hello,

I've never used Javascript before (my site is basically all in PHP), and I wanted 5 news articles to rotate on the front page eg. first news will show, 5 seconds later it will switch to the second etc.

I was lucky enough to find a script that I figured out how to modify to my needs and it's working fine, however I'd also like people to be able to manually scroll through them. I want there to be '1 - 2 - 3 - 4 - 5' down the bottom, when people click / hover over them it will skip straight to that news article. Here's the code I have at the moment:

Expand|Select|Wrap|Line Numbers
  1. //pre-load images
  2. var listImg = new Image();
  3. listImg.src = 'watch.gif';
  4. var treeImg = new Image();
  5. treeImg.src = 'dhtml.gif';
  6. var formImg = new Image();
  7. formImg.src = 'form.gif';
  8. var autoImg = new Image();
  9. autoImg.src = 'web.gif';
  10.  
  11. function makeNews(c,l,f,i){
  12.     this.copy = c;
  13.     this.link = l;
  14.     this.follow = f;
  15.     this.img = i;
  16.     this.write = writeNews;
  17. }
  18.  
  19. function writeNews(){
  20.     var str = '';
  21.     str += this.copy + '<br>';
  22.         str +=  '<a href="' + this.link + '">' + this.follow + '</a>';
  23.     return str;
  24. }
  25.  
  26. var newsArray = new Array();
  27. newsArray[0] = new makeNews("News here",'link<br />','Full Article',listImg).write();
  28.  
  29. newsArray[1] = new makeNews("News Here",'link','Full Article',treeImg).write();
  30.  
  31. newsArray[2] = new makeNews("News Here",'link','Full Article',autoImg).write();
  32.  
  33. newsArray[3] = new makeNews("News Here",'link','Full Article',formImg).write();
  34.  
  35. newsArray[4] = new makeNews("News here",'link','Full Article',formImg).write();
  36.  
  37. var nIndex = 0;
  38. var timerID = null;
  39. function rotateNews(){
  40.     var len = newsArray.length;
  41.     if(nIndex >= len)
  42.         nIndex = 0;
  43.     document.getElementById('stories').innerHTML = newsArray[nIndex];
  44.     nIndex++;
  45.     timerID = setTimeout('rotateNews()',9000);
  46. }
  47. function pauseNews() {
  48.     if (timerID != null) {
  49.         clearTimeout(timerID);
  50.         timerID = null;
  51.     }
  52. }
  53.  
  54. function playNews() {
  55.     if (timerID == null) {
  56.         timerID = setTimeout('rotateNews()', 1000);
  57.     }
  58. }
  59.  
  60.  
Thanks for any help.
Mar 1 '08 #1
3 4567
acoder
16,027 Expert Mod 8TB
The first link could have the following code onclick:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('stories').innerHTML = newsArray[0]; return false;
If you want to create the links automatically, use a for loop to create links for each item in the news array.
Mar 1 '08 #2
Jeigh
73
Thank you very much acoder, it works great.
Mar 2 '08 #3
acoder
16,027 Expert Mod 8TB
You're welcome. Glad it worked.
Mar 2 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ian Hubling | last post by:
I'm trying to complete a rotating banner ad within a page I have. The rotating add has four images that rotate in three-second increments. I've got the images to rotate ok - but now I want to go...
1
by: Sandy Bremmer | last post by:
I have seen many Javascripts that rotate images with each load or refresh of the page but so far all I've found require hard coding the image filename into the script. Does anyone know of a script...
1
by: Grunt | last post by:
Hi, I have been trying to put together a rotating banner. the code works but I am having a problem with the caching of the banner images. no matter what I try the page is constantly reloading the...
31
by: Royal Denning | last post by:
I am designing a table with 2 columns and 20 rows. I want to insert small images (each with a link) and a text title in each cell of the table. On refresh of the page, I would like to have the...
14
by: mikeoley | last post by:
Why would this script below work on an html page: http://www.eg-designdev.com/aircylinders/test.htm But not a java page such as this: "http://www.eg-designdev.com/aircylinders/index3.jsp" Or...
2
by: sgMuser | last post by:
Hi, I am not a good developer of Javascript codes. Needs this help to make some modification to this famous free javascript from Anarchos. i am using this in one of my webpage. What it does is,...
1
by: LeeUK | last post by:
I have a site that I run with 2 other people & we each have a google adsense account to help pay for the site. I want to be able to rotate our codes so that each of our ads appear evenly. I have...
1
by: AR123 | last post by:
Hi I want to set up a rotating banner. Not sure how to incorporate my rotating banner code into the code below. I want the rotating banner to be the main feature image? This is set up in...
6
by: swethak | last post by:
Hi, I displayed the image taken from database.How to raotate that image using javascript.plz tell that how to start the logic.plz tell that some reference websites.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.