{"id":709,"date":"2013-04-29T14:13:46","date_gmt":"2013-04-29T14:13:46","guid":{"rendered":"http:\/\/javascript-tutor.net\/?page_id=709"},"modified":"2019-03-11T23:40:43","modified_gmt":"2019-03-11T23:40:43","slug":"lesson-29-creating-slideshow-in-javascript","status":"publish","type":"page","link":"https:\/\/javascript-tutor.net\/index.php\/lesson-29-creating-slideshow-in-javascript\/","title":{"rendered":"JavaScript Lesson 29: Creating Slideshow"},"content":{"rendered":"<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-28-managing-banner-ads-in-javascript\/\">&lt;Previous Lesson&gt;<\/a>\u00a0<a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-tutorial\/\">[Table of Contents]<\/a>\u00a0<a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-30-creating-rollover-effects-in-javascript\/\">&lt;Next Lesson&gt;<\/a><\/strong><\/h4>\n<p>In lesson 28, we have learned how to create rotating banners without links and with links. In this lesson, we will modify the JavaScript code for the rotating banners into a slideshow.The JavaScript code for the slideshow is almost similar to the JavaScript code of the rotating banners but it gives control to the user to choose the banner ads he or she wants to see by clicking on the forward and backward buttons.<\/p>\n<div id=\"mycontent\">\n<div class=\"column1\">\n<p>To create the JavaScript slideshow, first of all, you need to create a few banner images using some graphics tools, or you can snap some photos with your digital camera or your smartphone. In our example, we have created four images and we named them as banner1.jpg,\u00a0banner2.jpg,\u00a0banner3.jpg, and\u00a0banner4.jpg. Save the images in the same folder as the HTML file slideshow.htm which will be used to show the slides.<\/p>\n<p><span style=\"font-size: medium;\"><b>Pro Programming Tip :<\/b><\/span><span style=\"font-size: medium;\">\u00a0Get a single click access to remote access your essential programming tool kit online with\u00a0<\/span><a href=\"https:\/\/www.clouddesktoponline.com\/citrix-xen-desktop\/\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"font-size: medium;\">citrix vdi<\/span><\/a><span style=\"font-size: medium;\">\u00a0from CloudDesktopOnline to remotely catch up with your programming work from anywhere, anytime on any device(PC\/Mac\/Linux\/android\/iOS). Learn more about\u00a0<a href=\"https:\/\/www.o365cloudexperts.com\/office-365-e1\/\" target=\"_blank\" rel=\"noopener noreferrer\">Enterprise E1<\/a>\u00a0suite and\u00a0<\/span><a href=\"http:\/\/www.apps4rent.com\/managed-azure.html\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"font-size: medium;\">Azure managed services<\/span><\/a><span style=\"font-size: medium;\">\u00a0by visiting Apps4Rent.<\/span><\/p>\n<\/div>\n<div class=\"column2\">\n<script src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\" async=\"\"><\/script><\/p>\n<p><!-- Javascript Responsive Ads --><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block;\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"9005101105\" data-ad-format=\"rectangle\"><\/ins><br \/>\n<script><br \/> (adsbygoogle = window.adsbygoogle || []).push({});<br \/> <\/script><\/div>\n<\/div>\n<p>In the slideshow JavaScript code, we create an array MySlides to store the banner images using the following statement:<\/p>\n<p><span class=\"outputbg\">MySlides=new Array(&#8216;banner1.jpg&#8217;,&#8217;banner2.jpg&#8217;,&#8217;banner3.jpg&#8217;,&#8217;banner4.jpg&#8217;)<\/span><\/p>\n<p>We use the variable Slide to indicate the index of the above array, where 0 represents the index of the first slide, 1 represents the index of the second slide and so on. We also created the function\u00a0ShowSlides(SlideNumber) with the argument SlideNumber.<\/p>\n<p>To call this function, we created two buttons within the body tag, the Forward button, and the Back button. Clicking the Forward will pass the value of 1 to the argument SlideNumber and clicking the Back button will pass the value of -1 to the argument SlideNumber. Using the statement:<\/p>\n<p><span class=\"outputbg\">Slide=Slide+SlideNumber<\/span><\/p>\n<p>We can then load the next banner image or the previous banner image.<\/p>\n<p>The statement<\/p>\n<div class=\"outputbg\" style=\"width: 40%;\">if (Slide&gt;MySlides.length-1){<br \/>\nSlide=0<\/div>\n<p>is to go back to the first image and avoid the error of loading the next image after the final image has been reached. Similarly, the statement<\/p>\n<div class=\"outputbg\" style=\"width: 40%;\">if (Slide&lt;0) {<br \/>\nSlide=MySlides.length-1<\/div>\n<p>is to fast forward to the last image and avoid the error of loading the image with the negative index<\/p>\n<p>The following Statement is to display the current image by referencing the name of the<br \/>\n&lt;img&gt; tag.<\/p>\n<p><span class=\"outputbg\">document.DisplaySlide.src=MySlides[Slide]<\/span><\/p>\n<div class=\"codebg\">\n<h4><strong>The Slideshow Script<\/strong><\/h4>\n<p>&lt;!DOCTYPE html PUBLIC &#8220;-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN&#8221;&gt;<br \/>\n&lt;html xmlns=&#8221;http:\/\/www.w3.org\/1999\/xhtml&#8221;&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;script language=&#8221;Javascript&#8221;&gt;<br \/>\nMySlides=new Array(&#8216;banner1.jpg&#8217;,&#8217;banner2.jpg&#8217;,&#8217;banner3.jpg&#8217;,&#8217;banner4.jpg&#8217;)<br \/>\nSlide=0<br \/>\nfunction ShowSlides(SlideNumber){<\/p>\n<p>{ Slide=Slide+SlideNumber<br \/>\nif (Slide&gt;MySlides.length-1){<br \/>\nSlide=0<br \/>\n}<br \/>\nif (Slide&lt;0) {<br \/>\nSlide=MySlides.length-1<br \/>\n}<br \/>\ndocument.DisplaySlide.src=MySlides[Slide]<br \/>\n}<br \/>\n}<br \/>\n&lt;\/script&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;P align=&#8221;center&#8221;&gt;&lt;img src=&#8221;banner1.jpg&#8221; name=&#8221;DisplaySlide&#8221; width=&#8221;900&#8243; height=&#8221;120&#8243; \/&gt;&lt;p&gt;<br \/>\n&lt;center&gt;<br \/>\n&lt;table border=0&gt;<br \/>\n&lt;tr&gt;<br \/>\n&lt;td align=center&gt;<br \/>\n&lt;input type=&#8221;button&#8221; value=&#8221;Back&#8221; onclick=&#8221;ShowSlides(-1)&#8221;&gt;<br \/>\n&lt;input type=&#8221;button&#8221; value=&#8221;Forward&#8221; onclick=&#8221;ShowSlides(1)&#8221;&gt;<\/p>\n<p>&lt;\/td&gt;<br \/>\n&lt;\/tr&gt;<br \/>\n&lt;\/table&gt;<br \/>\n&lt;\/center&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/p>\n<\/div>\n<p>View <a href=\"http:\/\/javascript-tutor.net\/java_tutor\/image\/slideshow.htm\">SlideShow<\/a> in action.<br \/>\n<script src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\" async=\"\"><\/script><\/p>\n<p><!-- Javascript Responsive Ads --><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block;\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"9005101105\" data-ad-format=\"auto\"><\/ins><br \/>\n<script><br \/> (adsbygoogle = window.adsbygoogle || []).push({});<br \/> <\/script><\/p>\n<h4 style=\"text-align: center;\">\u00a0\u00a0<strong><a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-28-managing-banner-ads-in-javascript\/\">&lt;Previous Lesson&gt;<\/a>\u00a0<a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-tutorial\/\">[Table of Contents]<\/a>\u00a0<strong><a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-30-creating-rollover-effects-in-javascript\/\">&lt;Next Lesson&gt;<\/a><\/strong><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>&lt;Previous Lesson&gt;\u00a0[Table of Contents]\u00a0&lt;Next Lesson&gt; In lesson 28, we have learned how to create rotating banners without links and with links. In this lesson, we will modify the JavaScript code for the rotating banners into a slideshow.The JavaScript code for the slideshow is almost similar to the JavaScript code of the rotating banners but it &hellip; <a href=\"https:\/\/javascript-tutor.net\/index.php\/lesson-29-creating-slideshow-in-javascript\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;JavaScript Lesson 29: Creating Slideshow&#8221;<\/span><\/a><\/p>\n","protected":false},"author":35895,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-709","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Lesson 29: Creating Slideshow - Learn JavaScript Online | Free Interactive JavaScript Tutorials<\/title>\n<meta name=\"description\" content=\"This JavaScript lesson illustrates how to create a slideshow\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/javascript-tutor.net\/index.php\/lesson-29-creating-slideshow-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Lesson 29: Creating Slideshow - Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"og:description\" content=\"This JavaScript lesson illustrates how to create a slideshow\" \/>\n<meta property=\"og:url\" content=\"https:\/\/javascript-tutor.net\/index.php\/lesson-29-creating-slideshow-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"article:modified_time\" content=\"2019-03-11T23:40:43+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-29-creating-slideshow-in-javascript\/\",\"url\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-29-creating-slideshow-in-javascript\/\",\"name\":\"JavaScript Lesson 29: Creating Slideshow - Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/javascript-tutor.net\/#website\"},\"datePublished\":\"2013-04-29T14:13:46+00:00\",\"dateModified\":\"2019-03-11T23:40:43+00:00\",\"description\":\"This JavaScript lesson illustrates how to create a slideshow\",\"breadcrumb\":{\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-29-creating-slideshow-in-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/javascript-tutor.net\/index.php\/lesson-29-creating-slideshow-in-javascript\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-29-creating-slideshow-in-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/javascript-tutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Lesson 29: Creating Slideshow\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/javascript-tutor.net\/#website\",\"url\":\"https:\/\/javascript-tutor.net\/\",\"name\":\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"description\":\"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Lesson 29: Creating Slideshow - Learn JavaScript Online | Free Interactive JavaScript Tutorials","description":"This JavaScript lesson illustrates how to create a slideshow","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/javascript-tutor.net\/index.php\/lesson-29-creating-slideshow-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Lesson 29: Creating Slideshow - Learn JavaScript Online | Free Interactive JavaScript Tutorials","og_description":"This JavaScript lesson illustrates how to create a slideshow","og_url":"https:\/\/javascript-tutor.net\/index.php\/lesson-29-creating-slideshow-in-javascript\/","og_site_name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","article_modified_time":"2019-03-11T23:40:43+00:00","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/javascript-tutor.net\/index.php\/lesson-29-creating-slideshow-in-javascript\/","url":"https:\/\/javascript-tutor.net\/index.php\/lesson-29-creating-slideshow-in-javascript\/","name":"JavaScript Lesson 29: Creating Slideshow - Learn JavaScript Online | Free Interactive JavaScript Tutorials","isPartOf":{"@id":"https:\/\/javascript-tutor.net\/#website"},"datePublished":"2013-04-29T14:13:46+00:00","dateModified":"2019-03-11T23:40:43+00:00","description":"This JavaScript lesson illustrates how to create a slideshow","breadcrumb":{"@id":"https:\/\/javascript-tutor.net\/index.php\/lesson-29-creating-slideshow-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/javascript-tutor.net\/index.php\/lesson-29-creating-slideshow-in-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/javascript-tutor.net\/index.php\/lesson-29-creating-slideshow-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/javascript-tutor.net\/"},{"@type":"ListItem","position":2,"name":"JavaScript Lesson 29: Creating Slideshow"}]},{"@type":"WebSite","@id":"https:\/\/javascript-tutor.net\/#website","url":"https:\/\/javascript-tutor.net\/","name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","description":"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/709","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/users\/35895"}],"replies":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=709"}],"version-history":[{"count":31,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/709\/revisions"}],"predecessor-version":[{"id":2628,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/709\/revisions\/2628"}],"wp:attachment":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}