jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 如何在vue.js中使用侦听器来处理滚动和窗口大小调整等事件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
您好我正在学习vuejs并且正在将我的一个项目转换为vuejs我想知道我可以在方法中编写我的自定义函数并在挂钩中调用那些我想知道如何在vuejs中使用监听器.

也可以通过导入vue项目来使用我的jquery

vue网站上的事件监听器文档仅声明了v-on和click示例,但没有关于Windows监听器的示例

jQuery(document).ready(function(){
    //cache DOM elements
    var mainContent = $('.cd-main-content'),header = $('.cd-main-header'),sidebar = $('.cd-side-nav'),sidebartrigger = $('.cd-nav-trigger'),topNavigation = $('.cd-Top-Nav'),searchForm = $('.cd-search'),accounTinfo = $('.account');

    //on resize,move search and top nav position according to window width
    var resizing = false;
    moveNavigation();
    $(window).on('resize',function(){
        if( !resizing ) {
            (!window.requestAnimationFramE) ? setTimeout(moveNavigation,300) : window.requestAnimationFrame(moveNavigation);
            resizing = true;
        }
    });

    //on window scrolling - fix sidebar nav
    var scrolling = false;
    checkScrollbarPosition();
    $(window).on('scroll',function(){
        if( !scrolling ) {
            (!window.requestAnimationFramE) ? setTimeout(checkScrollbarPosition,300) : window.requestAnimationFrame(checkScrollbarPosition);
            scrolling = true;
        }
    });

    //mobile only - open sidebar when user clicks the hamburger menu
    sidebartrigger.on('click',function(event){
        event.preventDefault();
        $([sidebar,sidebartrigger]).toggleClass('nav-is-visible');
    });

    //click on item and show submenu
    $('.has-children > a').on('click',function(event){
        var mq = checkMQ(),SELEctedItem = $(this);
        if( mq == 'mobile' || mq == 'tablet' ) {
            event.preventDefault();
            if( SELEctedItem.parent('li').hasClass('SELEcted')) {
                SELEctedItem.parent('li').removeClass('SELEcted');
            } else {
                sidebar.find('.has-children.SELEcted').removeClass('SELEcted');
                accounTinfo.removeClass('SELEcted');
                SELEctedItem.parent('li').addClass('SELEcted');
            }
        }
    });

    //click on account and show submenu - desktop version only
    accounTinfo.children('a').on('click',SELEctedItem = $(this);
        if( mq == 'desktop') {
            event.preventDefault();
            accounTinfo.toggleClass('SELEcted');
            sidebar.find('.has-children.SELEcted').removeClass('SELEcted');
        }
    });

    $(document).on('click',function(event){
        if( !$(event.target).is('.has-children a') ) {
            sidebar.find('.has-children.SELEcted').removeClass('SELEcted');
            accounTinfo.removeClass('SELEcted');
        }
    });

    //on desktop - differentiate between a user trying to hover over a dropdown item vs trying to navigat@R_675_9473@o a submenu's contents
    sidebar.children('ul').menuAim({
        activate: function(row) {
            $(row).addClass('hover');
        },deactivate: function(row) {
            $(row).removeClass('hover');
        },exitMenu: function() {
            sidebar.find('.hover').removeClass('hover');
            return true;
        },submenuSELEctor: ".has-children",});

    function checkMQ() {
        //check if mobile or desktop device
        return window.getComputedStyle(document.querySELEctor('.cd-main-content'),'::before').getPropertyValue('content').replace(/'/g,"").replace(/"/g,"");
    }

    function moveNavigation(){
        var mq = checkMQ();

        if ( mq == 'mobile' && topNavigation.parents('.cd-side-nav').length == 0 ) {
            detacHelements();
            topNavigation.appendTo(sidebar);
            searchForm.removeClass('is-hidden').prependTo(sidebar);
        } else if ( ( mq == 'tablet' || mq == 'desktop') &&  topNavigation.parents('.cd-side-nav').length > 0 ) {
            detacHelements();
            searchForm.insertAfter(header.find('.cd-logo'));
            topNavigation.appendTo(header.find('.cd-nav'));
        }
        checkSELEcted(mq);
        resizing = false;
    }

    function detacHelements() {
        topNavigation.detach();
        searchForm.detach();
    }

    function checkSELEcted(mq) {
        //on desktop,remove SELEcted class from items SELEcted on mobile/tablet version
        if( mq == 'desktop' ) $('.has-children.SELEcted').removeClass('SELEcted');
    }

    function checkScrollbarPosition() {
        var mq = checkMQ();

        if( mq != 'mobile' ) {
            var sidebarHeight = sidebar.outerHeight(),windowHeight = $(window).height(),mainContentHeight = mainContent.outerHeight(),scrollTop = $(window).scrollTop();

            ( ( scrollTop + windowHeight > sidebarHeight ) && ( mainContentHeight - sidebarHeight != 0 ) ) ? sidebar.addClass('is-fixed').css('bottom',0) : sidebar.removeClass('is-fixed').attr('style','');
        }
        scrolling = false;
    }
});

解决方法

您可以在Vue中监听窗口事件,如下所示:

@H_63_11@methods: { onResize(event) { console.log('window has been resized',event) } },mounted() { // Register an event listener when the Vue component is ready window.addEventListener('resize',this.onResizE) },beforeDestroy() { // Unregister the event listener before destroying this Vue instance window.removeEventListener('resize',this.onResizE) }

或者使用jQuery:

@H_63_11@methods: { onResize(event) { console.log('window has been resized',mounted() { // Register an event listener when the Vue component is ready $(window).bind('resize',beforeDestroy() { // Unregister the event listener before destroying this Vue instance $(window).unbind('resize',this.onResizE) }

大佬总结

以上是大佬教程为你收集整理的jquery – 如何在vue.js中使用侦听器来处理滚动和窗口大小调整等事件全部内容,希望文章能够帮你解决jquery – 如何在vue.js中使用侦听器来处理滚动和窗口大小调整等事件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。