0

我试图让图片在鼠标滚轮上垂直滚动。如何使它在起点时不向后滚动,从第一张到最后一张图片,并且在最后一张图片上不进一步滚动?

var myimages=[
            "https://d248fncte96e1e.cloudfront.net/xs/0137.jpg",
            "https://d248fncte96e1e.cloudfront.net/xs/0138.jpg",
            "https://d248fncte96e1e.cloudfront.net/xs/0139.jpg",
            "https://d248fncte96e1e.cloudfront.net/xs/0140.jpg",
            "https://d248fncte96e1e.cloudfront.net/xs/0141.jpg",
            "https://d248fncte96e1e.cloudfront.net/xs/0142.jpg",
            "https://d248fncte96e1e.cloudfront.net/xs/0143.jpg",
            "https://d248fncte96e1e.cloudfront.net/xs/0144.jpg",
            "https://d248fncte96e1e.cloudfront.net/xs/0145.jpg",
            "https://d248fncte96e1e.cloudfront.net/xs/0146.jpg",
            "https://d248fncte96e1e.cloudfront.net/xs/0147.jpg",
            "https://d248fncte96e1e.cloudfront.net/xs/0148.jpg",
            "https://d248fncte96e1e.cloudfront.net/xs/0149.jpg"                
        ]

        var slideshow=document.getElementById("slideshow")
        var nextslideindex=0

        function rotateimage(e){
            var evt=window.event || e //equalize event object
            var delta=evt.detail? evt.detail*(-120) : evt.wheelDelta //delta returns +120 when wheel is scrolled up, -120 when scrolled down
            nextslideindex=(delta<=-120)? nextslideindex+1 : nextslideindex-1 //move image index forward or back, depending on whether wheel is scrolled down or up
            nextslideindex=(nextslideindex<0)? myimages.length-1 : (nextslideindex>myimages.length-1)? 0 : nextslideindex //wrap image index around when it goes beyond lower and upper boundaries
            slideshow.src=myimages[nextslideindex]
            if (evt.preventDefault) //disable default wheel action of scrolling page
                evt.preventDefault()
            else
                return false

        }

        var mousewheelevt=(/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel" //FF doesn't recognize mousewheel as of FF3.x

        if (slideshow.attachEvent) //if IE (and Opera depending on user setting)
            slideshow.attachEvent("on"+mousewheelevt, rotateimage)
        else if (slideshow.addEventListener) //WC3 browsers
            slideshow.addEventListener(mousewheelevt, rotateimage, false)
});
4

1 回答 1

0

您需要检查数组中是否有下一个图像。这意味着在设置图像之前添加 if 语句,如果未定义,则返回第一张图像并重置计数器

var slideshow=document.getElementById("slideshow")
var nextslideindex=0

function rotateimage(e){
        var evt=window.event || e //equalize event object
        var delta=evt.detail? evt.detail*(-120) : evt.wheelDelta //delta returns +120 when wheel is scrolled up, -120 when scrolled down
        nextslideindex=(delta<=-120)? nextslideindex+1 : nextslideindex-1 //move image index forward or back, depending on whether wheel is scrolled down or up
        nextslideindex=(nextslideindex<0)? myimages.length-1 : (nextslideindex>myimages.length-1)? 0 : nextslideindex //wrap image index around when it goes beyond lower and upper boundaries
        if(myimages[nextslideindex] != undefined) {
            slideshow.src=myimages[nextslideindex]
        }
        else {
            slideshow.src=myimages[0]
            nextslideindex = 0;
        }
        if (evt.preventDefault) //disable default wheel action of scrolling page
            evt.preventDefault()
        else
            return false

}

编辑

现在,我根据 jsFiddle 和评论了解了您的目标。你想简单地防止它超过终点。您可以通过简单地添加 if 语句并删除该行来轻松地做到这一点

nextslideindex=(nextslideindex<0)?...

更新的功能

var slideshow = document.getElementById("slideshow");
var nextslideindex = 0;

function rotateimage(e){
    var evt = window.event || e; //equalize event object
    var delta = evt.detail? evt.detail * (-120) : evt.wheelDelta; //delta returns +120 when wheel is scrolled up, -120 when scrolled down
    nextslideindex = (delta <= -120)? nextslideindex + 1 : nextslideindex - 1 //move image index forward or back, depending on whether wheel is scrolled down or up

    if(myimages[nextslideindex] != undefined) {
        slideshow.src = myimages[nextslideindex];
    }
    else {
        // You can put an alert or something here if you'd like
    }

    if (evt.preventDefault) //disable default wheel action of scrolling page
        evt.preventDefault();
    else
        return false;

}
于 2013-08-08T16:33:50.013 回答