Stop browser from auto scrolling when searching do

2019-08-03 22:27发布

问题:

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.