在$(window).resize(function(){...})被触发后,函数无法正常工作

时间:2017-03-28 00:51:42

标签: javascript jquery wordpress

我需要你的帮助。

我不知道我的代码中有什么问题,在初始加载时函数正常工作,但是当我调整窗口大小时,函数将无法正常工作。

这是我的js

var firedData = true;
    var pcNavigation = function() {
        $('body').find('.appendNavClick').remove();
    };

    var appendToggleButton = function() {
       $('nav').find('a[href^="#toggle"]').remove();
       $.each($('.navigation > li'), function(i, value) {
           if($(value).children('.sub-menu').length == 1) {
               var styleString = 'style="position: absolute; right: 0; top: 1px; background: #232323;"';
               $('<a class="appendNav appendNavClick" href="#toggle" '+ styleString +'><span></span></a>').insertAfter($(value).children('a'));
           }
       });
    };

    var spNavigation = function() {
       appendToggleButton();
       $('body').delegate('#menu-main-navigation li > a[href^="#toggle"]', 'click', function(e) {
           e.preventDefault();
           var self = $(this),
              nextSelector = self.next();
           if(self.parent('li').siblings().find('a[href^="#toggle"]').next().is(':visible')) {
              self.parent('li').siblings().find('a[href^="#toggle"]').toggleClass('sub-menu-open').next().slideUp();
           }
           self.toggleClass('sub-menu-open');
           nextSelector.stop().slideToggle();
       });
    };

    var initResponsive = function() {
       let getResponsive = $(window).width();
       $('.navigation li .sub-menu').removeAttr('style');
       if(getResponsive < 767) {
           spNavigation();
       } else {
           pcNavigation();
       }
    };

    initResponsive();
    $(window).bind('resize', function() {
        initResponsive();
    });

我也在使用wordpress框架。

1 个答案:

答案 0 :(得分:0)

我已使用df <- read.table(textConnection( 'Year Count 2016 1 2015 7 2014 8 2013 3 2012 13 2011 3 2010 6 2009 2 2008 2 2007 3 2006 1 2005 0 2004 1 2003 7 2002 1 2001 3 2000 0'), header = TRUE) hw <- theme_gray()+ theme( plot.title=element_text(hjust=0.5,face='bold',size=16), axis.title.y=element_text(angle=0,vjust=.5,face='bold',size=13), axis.title.x=element_text(face='bold',size=13), plot.subtitle=element_text(hjust=0.5), plot.caption=element_text(hjust=-.5), strip.text.y = element_blank(), strip.background=element_rect(fill=rgb(.9,.95,1), colour=gray(.5), size=.2), panel.border=element_rect(fill=FALSE,colour=gray(.70)), panel.grid.minor.y = element_blank(), panel.grid.minor.x = element_blank(), panel.spacing.x = unit(0.10,"cm"), panel.spacing.y = unit(0.05,"cm"), axis.ticks=element_blank(), axis.text=element_text(colour="black"), axis.text.y=element_text(margin=margin(0,3,0,3),face="bold",size=10), axis.text.x=element_text(margin=margin(-1,0,3,0),face="bold",size=10) ) dfnew=NULL for (row in 1:nrow(df)) { temp=rep(df[row,]$Year,df[row,]$Count) dfnew = rbind(dfnew,data.frame(Year=temp)) } df=dfnew library(ggplot2) ggplot(df,aes(x=Year)) + geom_histogram()+ labs(x="Year", y="Count", title="Year vs MLB No-Hitters Count")+hw ggplot(df,aes(x=Year)) + geom_histogram(binwidth=1, fill="cornsilk",color="black")+ labs(x="Year", y="Count", title="Year vs MLB No-Hitters Count")+hw ggplot(df,aes(x=Year,..density..)) + geom_histogram(binwidth=1, fill="cornsilk",color="black")+ labs(x="Year", y="Count", title="Year vs MLB No-Hitters Count")+hw histPlot <- ggplot(df,aes(x=Year,..density..))+ geom_histogram(binwidth=1, fill="cornsilk",color="black")+ labs(x="Year", y="Density", title="Year vs MLB No-Hitters Count")+hw histPlot histPlot + geom_freqpoly(binwidth=1,color="red",size=1.2) histPlot + geom_line(stat="density",color="blue",size=1.2)+ xlim(1999,2017) histPlot + geom_density(adjust=.4,fill="cyan",color="black",alpha=.40)+ xlim(1999,2017)

bind()unbind()函数解决了这个问题