简体   繁体   English

如何解决导航中的jQuery淡入淡出动画冲突?

[英]How do I resolve a jQuery fading animation conflict in my navigation?

I am currently working on a company website on Wordpress that has a navigation menu that would seamlessly load the link's content after fade in through the jQuery load() function. 我目前正在Wordpress上的公司网站上工作,该网站具有导航菜单,在通过jQuery load()函数淡入后,将无缝加载链接的内容。 Like so: 像这样:

$("#header .menu a, #header .logo, nav.menu_mobile a:not('.close')").bind('click',function () {

var href = $(this).attr("href");
var title = $(this).attr("title");

history.pushState(null, title, href);

$("#page").fadeOut(500, function() {
    $("#page").delay(500).empty();
    $(window).scrollTop(0);
    $("#header").removeClass("scrolled");
});

$('#page').load(href + " .content", function() {
    $('#page').fadeIn(500, function(){

        if ( $("#main").has(".posts") ) {
            setTimeout(function() {
                masonrygrid();
            }, 1000);
        }

        setTimeout(function() {
            $("body").getNiceScroll().resize();
        }, 500);

    });
});

return false;
});

Currently, I'm having problems executing the "Work" page, which is basically a portfolio page. 目前,我在执行“工作”页面时遇到问题,该页面基本上是一个投资组合页面。

There should be a modal box that would fade in when the posts are clicked and all of the images from the posts would be shown on the modal box. 当单击帖子时,应该有一个模式框会淡入,并且帖子中的所有图像都将显示在模式框上。

When the "Work" page is accessed through the address bar, the function (shown below) would work fine. 通过地址栏访问“工作”页面时,该功能(如下所示)可以正常工作。 However, when the page is accessed through the navigation menu, though, the function doesn't work. 但是,当通过导航菜单访问页面时,该功能不起作用。

$(".page-id-44 .post .meta").click(function() {
    $("#gallery").fadeIn();
    return false;
});

$("#gallery .close").click(function() {
    $("#gallery").fadeOut();
    return false;
});

Demo: trivecs.com (still under construction, though) 演示: trivecs.com (不过仍在建设中)

With Wordpress, you just use jQuery(document).ready(function($){ }); 使用Wordpress,您只需使用jQuery(document).ready(function($){ });

And then everything inside of that, you can use the $ like you normally would. 然后,其中的所有内容都可以像平常一样使用$。

First of all, by default $ won't work in WordPress as it is. 首先,默认情况下$不能按原样在WordPress中工作。 Because, jQuery supplied with WordPress is in noConflict mode to avoid issues with other libraries that may use the $ as their object reference. 因为,WordPress提供的jQuery处于noConflict模式,以避免其他可能将$用作对象引用的库的问题。

So wrap your all jquery code inside this. 因此,将所有jquery代码包装在其中。

(function($){
    // your code here
})(jQuery)

Update: 更新:

Above code will work if it's jQuery's issue ($ is not a function). 如果是jQuery的问题(​​$不是函数),则上述代码将起作用。 If there's other issue, post error in question from console. 如果还有其他问题,请从控制台发布有问题的错误。

Update2: UPDATE2:

I found the issue on your website: Change code for work page like below. 我在您的网站上发现了这个问题:更改工作页面的代码,如下所示。

It's event delegation. 这是事件委托。 When page loads you don't have those class in your DOM, so when you navigate via navigation then you are adding those dynamically. 当页面加载时,您的DOM中没有这些类,因此当您通过导航进行导航时,便会动态添加这些类。 So event delegation is for that dynamic classes in document . 因此,事件委托是针对document中的动态类的。 See this link 看到这个链接

$(document).on('click', ".page-id-44 .post .meta", function() {
    $("#gallery").fadeIn();
    return false;
});

$(document).on('click', "#gallery .close", function() {
    $("#gallery").fadeOut();
    return false;
});

Try this: 尝试这个:

jQuery(document).ready(function( $ ) {
    // $ Works! You can test it with next line if you like
    // console.log($);
});

Try jQuery of all " $ ". 尝试所有“ $”的jQuery。

 jQuery("#header .menu a, #header .logo, nav.menu_mobile a:not('.close')").bind('click',function () { var href = jQuery(this).attr("href"); var title = jQuery(this).attr("title"); history.pushState(null, title, href); jQuery("#page").fadeOut(500, function() { jQuery("#page").delay(500).empty(); jQuery(window).scrollTop(0); jQuery("#header").removeClass("scrolled"); }); jQuery('#page').load(href + " .content", function() { jQuery('#page').fadeIn(500, function(){ if ( jQuery("#main").has(".posts") ) { setTimeout(function() { masonrygrid(); }, 1000); } setTimeout(function() { jQuery("body").getNiceScroll().resize(); }, 500); }); }); return false; }); 

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

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