Stop browser from auto scrolling when searching document (ctrl + f)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 08:39:10

问题


I have a website that uses jquery to scroll around in a defined height/width box.

This works great, until you try to Ctrl + F search. Then, it moves around erratically, stopping halfway between pages, and moving my slideshows between slides. This completely breaks it and needs to be reloaded for the functionality to return.

Is there any way to disable this?


回答1:


you could make it so that Find won't find the words! One way to do this would be to use the js:

 window.addEventListener("keydown",function (e) {
    if (e.keyCode === 114 || (e.ctrlKey && e.keyCode === 70)) { 
        e.preventDefault();
    }
})

(however there is a menu Find option ;) but It is possible to override default browser shortcuts, and there are some more than valid times do it too... just take JSfiddle or Google Docs for example, )




回答2:


No, you can not disable that.

you only can scroll to nearest slide on every scroll event.

// fill lidesTopOffsets array with top offsets
// of your slides when document is ready
var slidesTopOffsets = [100, 200, 300, 400, 500];

var minDifferecne = 10000;

$( window ).scroll(function() {
    // find nearest slide
    for(var i=0; i < slidesTopOffsets.length; i++){
        if(Math.abs($(window).scrollTop() - slidesTopOffsets[i]) < minDifferecne)
            minDifferecne = slidesTopOffsets[i];
    }

    // scroll to nearest slide
    $(body).animate({
        scrollTop: minDifferecne
    }, 0);
});

hope that it helps.



来源:https://stackoverflow.com/questions/19941641/stop-browser-from-auto-scrolling-when-searching-document-ctrl-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!