Solved

javascript slider working ?

Posted on 2009-05-07
4
421 Views
Last Modified: 2012-08-14
following code is the javascript code for slider horizontal.
i need to know how this code is working ?
anybody can explain will be grateful .

i know something  javascript but this is a bit more complex.

Thanks
$.fn.drag_slider = function (wrapperSelector) {
    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  
    return this.each(function () {
        var $wrapper = $(wrapperSelector, this),
            $slider = $wrapper.find('ul'),
            $items = $slider.find('li'),
            $single = $items.filter(':first'),
            
            singleWidth = $single.outerWidth(), //parseInt($single.width()) + parseInt($single.css('padding-left')) + parseInt($single.css('padding-right')),
            visible = Math.ceil($wrapper.innerWidth() / singleWidth)    <!--note: doesnt include padding or border-->
            currentPage = 1,
            pages = Math.ceil($items.length / visible);            
 
        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        if (($items.length % visible) != 0) {
            $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            $items = $slider.find('> li');
        }
 
        // 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
        $items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
        $items = $slider.find('> li'); // reselect
        
        // 3. Set the left position to the first 'real' item
        // $wrapper.css('scrollLeft', singleWidth * visible); // * -1
        $wrapper.get(0).scrollLeft = singleWidth * visible;
        
        // 4. Bind to the forward and back buttons
        $('a.back', this).click(function () {
            return gotoPage(currentPage - 1);                
        });
        
        $('a.forward', this).click(function () {
            return gotoPage(currentPage + 1);
        });
        
        // 5. paging function
        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1, // page < currentPage ? 1 : -1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;
           // alert(page);
            // console.log('currentPage', currentPage, 'left', left, 'n', n);
            
            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 500, function () {
                if (page == 0) {
                    currentPage = pages;
                    // $wrapper.css('scrollLeft', singleWidth * visible * pages); //  * -1
                    this.scrollLeft = singleWidth * visible * pages;
                } else if (page > pages) {
                    currentPage = 1;
                    this.scrollLeft = singleWidth * visible; //  * -1
                    // $wrapper.css('scrollLeft', singleWidth * visible); //  * -1
                    // reset back to start position
                } else {
                    currentPage = page;
                }
            });                
            
            return false;
        }
        
        // create a public interface to move to a specific page
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
    });  
};
 
$(function () {
    $('#scrollButtons > div').before('<a href="#" class="back"><img src="images/panel-arrow-l.jpg" width="28" height="52" class="scrollLeft" /></a>').after('<a class="forward" href="#"><img src="images/panel-arrow-r.jpg" width="28" height="52"  class="scrollRight" /></a>').parent().drag_slider('> div');
});

Open in new window

0
Comment
Question by:hiddenpearls
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:alien109
ID: 24325091
is there a certain area that you are confused on? maybe you can be more specific.
0
 

Author Comment

by:hiddenpearls
ID: 24326104
$(function () {
    $('#scrollButtons > div').before('<a href="#" class="back"><img src="images/panel-arrow-l.jpg" width="28" height="52" class="scrollLeft" /></a>').after('<a class="forward" href="#"><img src="images/panel-arrow-r.jpg" width="28" height="52"  class="scrollRight" /></a>').parent().drag_slider('> div');
});

this part is used to display two scroll buttons  at left and right side.
what is this parent().drag_slider('> div'); ?  in the above part.

this is the starting point of the script i think so .
0
 
LVL 12

Accepted Solution

by:
alien109 earned 500 total points
ID: 24326958
$('#scrollButtons > div').before('<a href="#" class="back"><img src="images/panel-arrow-l.jpg" width="28" height="52" class="scrollLeft" /></a>').after('<a class="forward" href="#"><img src="images/panel-arrow-r.jpg" width="28" height="52"  class="scrollRight" /></a>').parent().drag_slider('> div');

This line selects div tags which are direct descendants of the scrollButtons object and then inserts the html for creating the buttons before and after the element. It then calls the parent of the selected element, and executes the drag_slider function on div tags which are direct descendants (essentially itself).

The code structure is actually a bit redundant.

0
 

Author Comment

by:hiddenpearls
ID: 24327664
can there be any alternate to this above code ?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Tools for analysis of code 4 44
Duplicate Elements with JS / jQuery 3 24
15 puzzle in Javascript/html 2 36
javascript and jquery question 4 21
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question