Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a folder with over 500 images, I want to display the images into a slider. usually the long method is to write the names of all the 500 files and then get it displayed bu then i want to increase the images sizes to lets say over 1000 then i have to write another 500 names and this continue. so another way is to use a script to get all the names of images starting with img_ and store them into an array and then randomize the array when u press the next button on the slider and display it. i am not able to do that. nothing is being displayed. help please.

JavaScript
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<!-- directorySlider Javascript file -->
<script src="js/directorySlider.js"></script>
<script src="js/directorySlider.min.js"></script>
<script>
    $('#slider-main').directorySlider({
        animation: 'fade',
        filebase: 'img_',
        directory: 'images/',
        extension: 'jpg',
        numslides: 5,
        height: 240, 
        width:  320,
        timeout: 2000
    })
 </script>
 </head>
 <body>
<div class="directorySlider" id="slider-main"></div>
</body>
</html>


What I have tried:

JavaScript
/**
 * directorySlider v0.9 - Loads all images in a specified directory and creates a slide show
 * Author: Justin W. Hall
 * http://www.justinwhall.com/directory-jquery-slider/
 *
 * License: GPL v3
 */

(function($){
   var directorySlider = function(element, options)
   {
       var elem = $(element),
           obj = this,
           elemId = elem[0].id;

       // Merge config settings
       var config = $.extend({
           animation: 'slide',
           filebase: 'slide_',
           extension: 'jpg',
           speed: 1000,
           timeout: 4000,
           directory: null,
           numslides: null,
           height: null,
           width: null
       }, options || {});

       // set slideshow dimensions if set
       if (config.height) {
        $(elem).css('height', config.height);
       }
       if (config.width) {
        $(elem).css('width', config.width);
       }

       $(elem).css('overflow', 'hidden');

       // Get slides
       var slides = [],
       slideNumber = 1;

       while(slideNumber <= config.numslides){
         slides.push('<img src="' + config.directory + config.filebase + slideNumber + '.' + config.extension + '" />');
         slideNumber++;
       }

       // append slideshow
       // apply slide wrap 1st
       var slideWrap = $('<div class="' + elemId + '-slide-wrap"></div>');
           slideWrap.appendTo(elem);

        // append slide and position absolutley
       $.each(slides, function(index, val) {
         $(val).css({
           position: 'absolute',
           top: 0,
           left: 0,
           width: config.width // ADDED THIS SO WE DON'T NEED TO HAVE ALL IMAGES WITH SAME HEIGHT & WIDTH
         }).appendTo(slideWrap);
       });

    setInterval(function(){
       var firstSlide = elem.find('img:first-child'),
           lastSlide = elem.find('img:last-child');
       // Apply animation
       switch(config.animation){

        case 'fade':
            $(lastSlide).animate({
              opacity: 0},
              config.speed, function() {
              $(this).insertBefore(firstSlide).css('opacity', 1);
            });
        break;

        case 'uncover':
            lastSlide.animate({
              marginLeft: -$(this).width()},
              config.speed, function() {
              $(this).insertBefore(firstSlide).css('marginLeft', 0);
            });
            break;
        default:
            $(lastSlide).animate({
              opacity: 0},
              config.speed, function() {
              $(this).insertBefore(firstSlide).css('opacity', 1);
            });
       }
    }, config.timeout);

   };

   $.fn.directorySlider = function(options)
   {
       return this.each(function()
       {
           var element = $(this);

           // Return early if this element already has a plugin instance
           if (element.data('directoryslider')) return;

           // pass options to plugin constructor
           var directoryslider = new directorySlider(this, options);

           // Store plugin object in this element's data
           element.data('directoryslider', directoryslider);

       });
   };
})(jQuery);

this is the javascript. still it won't show any output
Posted
Updated 5-Apr-16 23:20pm
v3
Comments
Sergey Alexandrovich Kryukov 5-Apr-16 2:55am    
Code dump. Nothing to talk about. What to look at, without knowing the goal of it, anything? And why..?
—SA
spyketrash141 5-Apr-16 23:58pm    
i have a folder with over 500 images , i want to display the images into a slider. usually the long method is to write the names of all the 500 files and then get it displayed bu then i want to increase the images sizes to lets say over 1000 then i have to write another 500 names and this continue. so another way is to use a script to get all the names of images starting with img_ and store them into an array and then randomize the array when u press the next button on the slider and display it. i am not able to do that. nothing is being displayed. help please.
F-ES Sitecore 5-Apr-16 5:10am    
Have you followed the implementation instructions? Do you have the right files with the right filenames in the right folder of your webserver? Your question indicates you don't know how to implement this plugin so the documentation is usually the first place to start.
spyketrash141 5-Apr-16 23:59pm    
yes i followed and i have the files names correct and i am not missing any files , it still displays a blank webpage.
F-ES Sitecore 6-Apr-16 4:37am    
Use the browser's dev tools (F12 in Chrome or IE) to inspect the img elements and look at their src attributes and see if you have images at those urls. Copy and paste the url in the src attribute to see if it works in the browser normally and go from there.

1 solution

From your sample code I could see you are using the code before the document was loaded or the div "slider-main".
HTML
<div class="directorySlider" id="slider-main"></div>


I created a quick test environment to see if that was the case.
I got it to work by placing the code in between:
JavaScript
$(document).ready(function () {
	//code here
})
;

Please have a read of:
$( document ).ready() | jQuery Learning Center[^]

Also ensure the code is pointing to the correct path.
Here is a "Quick Reminder About File Paths":
Quick Reminder About File Paths | CSS-Tricks[^].

Hope that helps you out.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900