为Image Slider创建一个Jquery插件

时间:2011-10-30 12:38:24

标签: jquery jquery-ui

我需要将此函数转换为Jquery插件,以便它可以处理卷轴中的任意数量的图像。或者只是任何数量的图像可用于此滑块。如果需要,您可以参考thirteen23.com。我正在尝试制作与网站上使用的滑块完全相同的滑块。我是Stack Overflow的新用户。请帮助,并告诉我使用本网站的正确方法。提前致谢。 Link to JsFiddle

-Akshay    


  <div class="main_view">
    <div class="window">
        <div class="image_reel">
            <a href="#">
                <img width="948" src="Images/Home/LIV.jpg" alt=""></a> <a href="#">
            <img width="948" src="Images/Home/JUM.jpg" alt=""></a> <a href="#">
            <img width="948" src="Images/Home/LOV.jpg" alt=""></a> <a href="#">
            <img width="948" src="Images/Home/BLI.jpg" alt=""></a>
        </div>
        <div class="pictureslide">
            <h2>
                Livestrong</h2>
            <p>
                Mobile donations to fight cancer and improve lives.</p>
            <a href="http://thirteen23.com/work/#livestrong-donation">See more</a>
        </div>
        <div class="pictureslide">
            <h2>
                Jumpshot</h2>
            <p>
                The lighter side of serious software.</p>
            <a href="/#jumpshot">See more</a>
        </div>
        <div class="pictureslide">
            <h2>
                Flashback</h2>
            <p>
                An exciting way to explore, stylize, and share your Facebook photos.</p>
            <a href="/#flashback">See more</a>
        </div>
        <div class="pictureslide">
            <h2>
                Love Universe</h2>
            <p>
                Blurring the line between product and lifestyle.</p>
            <a href="/#love-universe">See more</a>
        </div>
    </div>
    <div class="paging">
        <a href="#" rel="1">
            <img src="Images/Home/LIV_sm.jpg" alt=""></a> <a href="#" rel="2">
        <img src="Images/Home/JUM_sm.jpg" alt=""></a> <a href="#" rel="3">
        <img src="Images/Home/LOV_sm.jpg" alt=""></a> <a href="#" rel="4">
        <img src="Images/Home/BLI_sm.jpg" alt=""></a>
    </div>
</div>

jQuery代码

windowObj = {
    imageWidth: 925,
    imagesum: 4
};

var MainPageSlider = function(windowObj) {
    $(".paging a:first").addClass("active");
    //Get size of images, how many there are, then determin the size of the image reel.
    var imageWidth = windowObj.imageWidth;
    var imageSum = windowObj.imagesum + 1;
    var imageReelWidth = (imageWidth * imageSum);
    //Adjust the image reel to its new size
    $(".image_reel").css({
        'width': imageReelWidth
    });
    //Paging + Slider Function
    rotate = function() {
        var triggerID = $active.attr("rel") - 1; //Get number of times to slide
        var image_reelPosition = triggerID * (imageWidth + 30); //Determines the distance the image reel needs to slide
        $(".paging a").removeClass('active'); //Remove all active class
        $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
        //Slider Animation
        $(".image_reel").animate({
            left: -image_reelPosition
        }, 500);
    };
    //Rotation + Timing Event
    rotateSwitch = function() {
        play = setInterval(function() { //Set timer - this will repeat itself every 3 seconds
            $active = $('.paging a.active').next();
            if ($active.length === 0) { //If paging reaches the end...
                $active = $('.paging a:first'); //go back to first
            }
            rotate(); //Trigger the paging and slider function
        }, 7000); //Timer speed in milliseconds (3 seconds)
    };
    rotateSwitch(); //Run function on launch
    //On Hover
    $(".image_reel a").hover(function() {
        clearInterval(play); //Stop the rotation
    }, function() {
        rotateSwitch(); //Resume rotation
    });
    //On Click
    $(".paging a").click(function() {
        $active = $(this); //Activate the clicked paging
        //Reset Timer
        clearInterval(play); //Stop the rotation
        rotate(); //Trigger rotation immediately
        rotateSwitch(); // Resume rotation
        return false; //Prevent browser jump to link anchor tag
    });
};
MainPageSlider(windowObj)

0 个答案:

没有答案