简体   繁体   English

使用mousewheel jquery插件滚动速度

[英]scroll speed with mousewheel jquery plugin

I want to imitate the Google+ header with the search bar. 我想用搜索栏模仿Google+标头。 When you scroll down it goes to top:-60px and the second horizontal menu will be top:0 from top:60px and become the main top horizontal menu, while the one with top:-60px remains hidden until we scroll to top. 向下滚动时,它会转到top:-60px ,第二个水平菜单将从top:60px变为top:0 ,并成为主要的top水平菜单,而top:-60px菜单将一直隐藏,直到我们滚动到顶部。

I managed to do this, but it only works when I scroll slowly (with trackpad, OSX, Chrome33). 我设法做到了,但是只有当我缓慢滚动(使用触控板,OSX,Chrome33)时,它才能起作用。 I researched and found out the scroll speed depends on the hardware (touchpad, mouse), the OS and even on the browser. 我研究发现滚动速度取决于硬件(触摸板,鼠标),操作系统甚至浏览器。 I found mousewheel plugin, that aims to make the scrolling speed equal but I can't make it work. 我找到了mousewheel插件,该插件旨在使滚动速度相等,但我无法使其正常工作。

Here is the js code: ( The delta divisions I got from here: https://stackoverflow.com/a/16696129 ) 这是js代码:(我从这里得到的delta分区: https : //stackoverflow.com/a/16696129

<script type="text/javascript">
    gn_top_menu_featured = $('.gn-top-menu-featured'),
    gn_top_menu = $('.gn-top-menu'),
    hide_gn_top_menu_featured = 0,
    gn_top_menu_position = 44;
    $('body').on('mousewheel', function(event) {
        if( event.deltaX >= 40 )
            event.deltaX /= 40;
        if( event.deltaY >= 40 )
            event.deltaY /= 40;
        var sy = $('body').scrollTop();
        if ( sy >= hide_gn_top_menu_featured && sy <= gn_top_menu_position ) {
            gn_top_menu_featured.css('top', -sy);
            gn_top_menu.css('top', gn_top_menu_position-sy);
        }
        else {
            // whatever
        }
    });
</script>

I really want to get this working properly, thank in advance. 我真的很想让它正常工作,谢谢。 :) :)

Turns out i misunderstood your problem at first. 原来我一开始就误解了你的问题。 Here's another attempt at solving this. 这是解决此问题的另一种尝试。 I still might not be understanding you correctly, because I still don't need to control the mousewheel speed to make this work. 我可能仍然无法正确理解您,因为我仍然不需要控制鼠标滚轮的速度即可完成这项工作。 Here's the updated fiddle . 这是更新的小提琴

I use the $(window).scroll event to check the $(window).scrollTop() value and change the css class of the div. 我使用$(window).scroll事件检查$(window).scrollTop()值并更改div的CSS类。

$(window).scroll(function(){
    $("#nav").css("top", ($(window).scrollTop() < 60 ? (60 - $(window).scrollTop()) : 0) + 'px');
    if ($(window).scrollTop() > 60) {
        $("#nav").addClass('sub-60').text('WOOT!');
    }
    else {
        $("#nav").removeClass('sub-60').text('MAIN NAV');
    }
});

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

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