有没有办法滚动iframe,即使光标没有徘徊呢?

时间:2014-10-19 02:11:45

标签: javascript iframe scroll

也许你可以在滚轮事件上制作一个滚动iframe的javascript?我不是那么擅长javascript。

我找到了这个,但我仍然不知道如何定位iframe并滚动它: Get mouse wheel events in jQuery?

1 个答案:

答案 0 :(得分:0)

你在这里:http://jsfiddle.net/dsmeuzL5/

HTML:

<iframe id="myframe" src="//www.yourpage.com"></iframe>

JS:     var iframe = document.getElementById(&#39; myframe&#39;);

var MouseWheelHandler = function (e) {
    // cross-browser wheel delta
    var e = window.event || e; // old IE support
    var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
    console.log(delta);
    var doc = iframe.contentDocument ? iframe.contentDocument : (iframe.contentWindow ? iframe.contentWindow.document : iframe.document);
    doc.body.scrollTop = doc.body.scrollTop - (delta * 30);

    return false;
}


if (window.addEventListener) {
    // IE9, Chrome, Safari, Opera
    window.addEventListener("mousewheel", MouseWheelHandler, false);
    // Firefox
    window.addEventListener("DOMMouseScroll", MouseWheelHandler, false);
}
// IE 6/7/8
else window.attachEvent("onmousewheel", MouseWheelHandler);