简体   繁体   English

如何设置浏览器窗口的滚动条或div滚动条以使用animate和scrollTop增量滚动?

[英]How can I set my browser window's scrollbar or a div scrollbar to scroll in increments using animate and scrollTop?

The general idea to the site i am designing is to scroll through a set of menu items horizontally and incrementally underneath a static div that will magnify(increase dimensions and pt size) the contents of a menu items. 我正在设计的站点的总体思路是在静态div下方水平滚动地浏览一组菜单项,该菜单会放大(增加尺寸和pt大小)菜单项的内容。 I don't really need help with the magnify portion because i think it's as simple as adding a mag class to any of the menuItem divs that go underneath the static div. 我真的不需要放大部分的帮助,因为我认为这就像将mag类添加到静态div下方的任何menuItem div一样简单。 I have been messing with this for a few weeks and the code I have for incrementally scrolling, so far, is this: 我已经搞砸了几周,到目前为止,我拥有的用于逐步滚动的代码是:

   $(document).ready(function () {

    currentScrollPos = $('#scrollableDiv').scrollTop(120); //sets default scroll pos


    /*The incrementScroll function is passed arguments currentScrollPos and UserScroll which are variables that i have initiated earlier in the program, and then initiates a for loop. 
        -The first statement sets up the variables: nextScrollPos as equal to the currentScrollPos(which by default is 120px) plus 240px(the distance to next menuItem), prevScrollPos as equal to the currentScrollPos(which by default is 120px) minus 240px(the distance to next menuItem).  
        -The second Statement checks to see if the user has scrolled using var userScroll
        -The third statement sets: var CurrentScroll equal to the new scroll position and var userScroll to false*/

    function incrementScroll(currentScrollPos, userScroll) {
        for (var nextScrollPos = parseInt(currentScrollPos + 240, 10),
        prevScrollPos = parseInt(currentScrollPos - 240, 10); //end first statement
        userScroll == 'true'; console.log('dude'),   //end second statement and begining of third
        currentScrollPos = scrollTop(), userScroll = 'false') { 


            if (scrollTop() < currentScrollPos) {
                $('#scrollableDiv').animate({
                    scrollTop: (parseInt(prevScrollPos, 10))
                }, 200);
                console.log('scrolln up')
            } else if (scrollTop() > currentScrollPos) {
                $('#scrollableDiv').animate({
                    scrollTop: (parseInt(nextScrollPos, 10))
                }, 200);
                console.log('scrolln down')//fire when 
            }

        }
    }

    $('#scrollableDiv').scroll(function () {
        userScroll = 'true';
        _.debounce(incrementScroll, 200); //controls the amount of times the incrementScroll function is called
        console.log('straight scrolln')
    });
});

I have found a variety of solutions that are nigh close: such as a plugin that snaps to the next or previous div horizontally demo , another solution that also snaps and is based on setTimeout demo , but nothing that nails incrementally scrolling through divs. 我发现了各种接近的解决方案:例如,一个插件可以水平捕捉到下一个或上一个div 演示 ,另一个解决方案也可以捕捉并基于setTimeout 演示 ,但是没有什么可以逐步滚动div了。 I also found a way to control the rate at which a user may scroll through the menuItems using debounce which is included in the above code. 我还找到了一种方法,该方法可以使用上述代码中包含的反跳来控制用户滚动菜单项的速率。

The console.logs inside the loop do not fire when I demo the code in jsfiddle which leads me to believe the problem lies within the loop. 当我在jsfiddle中演示代码时,不会触发循环内的console.logs,这使我认为问题出在循环内。 I'm a noob though so it could be in syntax or anywhere else in the code for that matter. 我是一个菜鸟,所以可以用语法或代码中的其他任何方式。 Also in the second demo, i have provided the css for the horizontal static div, but the moment I put it in my html it keeps the js from working. 同样在第二个演示中,我为水平静态div提供了CSS,但是当我将其放在html中时,它使js无法正常工作。

I would like to write the code instead of using a plugin and any help would be appreciated! 我想编写代码而不是使用插件,任何帮助将不胜感激! Also, thank you ahead of time! 另外,请提前谢谢!

Try this fiddle. 试试这个小提琴。 Menu container height is 960px to show 4 menu items. 菜单容器的高度为960像素,可显示4个菜单项。 "Zoom" div is positioned absolutely at top. “缩放” div绝对位于顶部。 When you scroll mouse over this div, menu items shifts to top/bottom. 当您在该div上滚动鼠标时,菜单项将移至顶部/底部。 I had to add additional div to bottom to be able to scroll to last 3 menu items. 我必须在底部添加其他div才能滚动到最后3个菜单项。 JS code: JS代码:

jQuery(document).ready(function($){

    var current = 0;
    var menu = $('.menu-container').scrollTop(0);
    var items = menu.find('.menu-item');
    var zoom = $('.zoom');


    function isVerticalScroll(event){
        var e = event.originalEvent;
        if (e.axis && e.axis === e.HORIZONTAL_AXIS)
            return false;
        if (e.wheelDeltaX)
            return false;
        return true;
    }

    function handleMouseScroll(event){
        if(isVerticalScroll(event)){
            var delta = event.originalEvent.wheelDelta * -1 || event.originalEvent.detail;
            current += (delta > 0 ? 1 : -1);

            if(current < 0)
                current = 0;
            if(current >= items.length){
                current = items.length - 1;
            }

            menu.stop().animate({
                "scrollTop": current * 240
            }, 300);

            items.removeClass('current').eq(current).addClass('current');

            event && event.preventDefault();
            return false;
        }
    }

    zoom.on({
        "MozMousePixelScroll": handleMouseScroll,
        "mousewheel": handleMouseScroll
    });
});

Hope it will help. 希望它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM