javascript - 如何在特定事件后停止 window.scroll() ?

标签 javascript jquery scroll fullscreen

我想让粘性导航在展开时与该网站的导航(http://amandagerhardsen.com/#cloudbusting/4)类似(菜单展开时滚动关闭)。

我该怎么做?

 var Boxlayout = (function () {
        var $el = $('#sticky-nav'),
            $sections = $el.children('section'),
            // work panels
            $workPanelsContainer = $('#bl-panel-work-items'),
            // close work panel trigger
            $closeWorkItem = $workPanelsContainer.find('nav > span.hidemenu'),
            transEndEventNames = {
                'WebkitTransition': 'webkitTransitionEnd',
                'MozTransition': 'transitionend',
                'OTransition': 'oTransitionEnd',
                'msTransition': 'MSTransitionEnd',
                'transition': 'transitionend'
            },
            // transition end event name
            transEndEventName = transEndEventNames[Modernizr.prefixed('transition')],
            // support css transitions
            supportTransitions = Modernizr.csstransitions;

        function init() {
            initEvents();
        }

        function initEvents() {
            $sections.each(function () {
                var $section = $(this);
                // expand the clicked section and scale down the others
                $section.on('click', function () {
                    if (!$section.data('open')) {
                        $section.data('open', true).addClass('bl-expand bl-expand-top');
                        $el.addClass('bl-expand-item');
                    }
                }).find('span.hidemenu').on('click', function () {
                    // close the expanded section and scale up the others
                    $section.data('open', false).removeClass('bl-expand').on(transEndEventName, function (event) {
                        if (!$(event.target).is('section')) return false;
                        $(this).off(transEndEventName).removeClass('bl-expand-top');
                    });
                    if (!supportTransitions) {
                        $section.removeClass('bl-expand-top');
                    }
                    $el.removeClass('bl-expand-item');
                    return false;
                });
            });
            // clicking on a work item: the current section scales down and the respective work panel slides up
            $workItems.on('click', function (event) {
                // scale down main section
                $sectionWork.addClass('bl-scale-down');
                // show panel for this work item
                $workPanelsContainer.addClass('bl-panel-items-show');
                var $panel = $workPanelsContainer.find("[data-panel='" + $(this).data('panel') + "']");
                currentWorkPanel = $panel.index();
                $panel.addClass('bl-show-work');
                return false;
            });
            // navigating the work items: current work panel scales down and the next work panel slides up
            $nextWorkItem.on('click', function (event) {
                if (isAnimating) {
                    return false;
                }
                isAnimating = true;
                var $currentPanel = $workPanels.eq(currentWorkPanel);
                currentWorkPanel = currentWorkPanel < totalWorkPanels - 1 ? currentWorkPanel + 1 : 0;
                var $nextPanel = $workPanels.eq(currentWorkPanel);
                $currentPanel.removeClass('bl-show-work').addClass('bl-hide-current-work').on(transEndEventName, function (event) {
                    if (!$(event.target).is('div')) return false;
                    $(this).off(transEndEventName).removeClass('bl-hide-current-work');
                    isAnimating = false;
                });
                if (!supportTransitions) {
                    $currentPanel.removeClass('bl-hide-current-work');
                    isAnimating = false;
                }
                $nextPanel.addClass('bl-show-work');
                return false;
            });
            // clicking the work panels close button: the current work panel slides down and the section scales up again
            $closeWorkItem.on('click', function (event) {
                // scale up main section
                $sectionWork.removeClass('bl-scale-down');
                $workPanelsContainer.removeClass('bl-panel-items-show');
                $workPanels.eq(currentWorkPanel).removeClass('bl-show-work');
                return false;
            });
        }
        return {
            init: init
        };
    })();

最佳答案

这是一个 fiddle :http://jsfiddle.net/77P2e/

完成后要小心再次解锁滚动,否则这可能会让用户感到非常烦人!

设置代码

var $window = $(window), previousScrollTop = 0, scrollLock = false;

$window.scroll(function(event) {     
    if(scrollLock) {
        $window.scrollTop(previousScrollTop); 
    }

    previousScrollTop = $window.scrollTop();

});

锁定滚动位置:

scrollLock = true;

并再次解锁...

scrollLock = false;

作为示例,您可以在鼠标进入导航区域时锁定窗口滚动位置,并在鼠标离开时再次解锁:

$("nav")
    .mouseenter(function(){ scrollLock = true; })
    .mouseleave(function(){ scrollLock = false; });

关于javascript - 如何在特定事件后停止 window.scroll() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21315306/

相关文章:

javascript - 在 React 中状态作为数组有什么问题?

javascript - 为什么这个属性赋值会覆盖 JavaScript 中先前的属性?

javascript - 用于访问下一个 html 页面的静态 4 位密码

jquery - 到达 div 时更改 ul 样式(滚动)

javascript - 处理错误后 IndexedDB 事务提交

javascript - 地铁 bundler : Cannot read properties of undefined (reading 'transformFile' )

jquery - 从不同元素调用 jQuery 插件时如何使用同一个实例?

javascript - datepicker和动态表不使用jQuery函数

xaml - 如何获取在WPF中创建的页面的垂直滚动和水平滚动

javascript - 监控移动 safari、iOS 6 和 7 上的实时滚动位置