添加幻灯片图像的链接,由滚轮javascript控制

时间:2013-03-11 22:11:15

标签: javascript javascript-events

我很难尝试在幻灯片中为每张图片添加不同的链接。

我对Javascript / Jquery相当新,所以如果有一种更简单有效的方法来实现这一目标,那就太棒了。

<img id="slideshow"  src="V1.jpg" />
<script type="text/javascript">
var myimages=[
    "V1.jpg", 
    "V2.jpg",
    "V3.jpg",
    "V4.jpg",
    "V5.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)
</script>

0 个答案:

没有答案