2014-07-04 34 views
0

有人能向我解釋時,它的定位一個類名不存在的事件將如何激發..的Javascript事件仍射擊,即使類名不存在

CODE

(function ($) { 

    var config = {}; 

    $(document).ready(function() { 

     var wi = $(window).width(); 

     if (wi > 768) { 
      $('body').addClass('dosomething'); 
     } else { 
      $('body').removeClass('dosomething'); 
     } 

     $(window).resize(function() { 

      var wi = $(window).width(); 

      console.log(wi); 

      if (wi > 768) { 
       $('body').addClass('dosomething'); 
      } else { 
       $('body').removeClass('dosomething'); 
      } 

      var $container = $('.email-signup__wrap'), 
      $cHeight = $('.email-signup').outerHeight(); 

      // $('.dosomething .email-signup__wrap').on('mouseenter mouseleave', function(ev) { 
      $('body').on('mouseenter mouseleave', '.dosomething .email-signup__wrap' , function(ev) { 

       var $this = $(this); 

       if (ev.type === 'mouseenter') { 

        TweenMax.to($container, .4, { 
         ease: Power2.easeOut, 
         css:{ 
          overflow: 'visible', 
          position: 'absolute', 
          top: -$cHeight 
         } 
        }); 
       } 
       else 
       { 
        TweenMax.to($container, .4, { 
         ease: Power2.easeOut, 
         css:{ 
          position: 'relative', 
          top: 0 
         }, 
         onComplete: hide 
        }); 

        function hide(){ 
         $container.css('overflow', 'hidden'); 
        } 

        $("div.mce_inline_error").remove(); 
       } 
      }); 
     }); 
    }); 

})(jQuery); 

我已經走了一段路,將dosomething類添加到每個選擇器,以防止在768px以下的屏幕尺寸上觸發事件。

基本上,用戶將鼠標懸停在頁腳欄上,並且聯繫表單會從畫布外彈出,但在較小的屏幕/移動設備上,它仍會保留在頁面內容的基礎上。

我知道這是最基本的代碼,但是,我一直在試圖讓這個工作好幾天,而且我正忙着搜索一些代碼以尋找解決方案。

我不是經過周折,媒體查詢等....我真的很想理解這一點,爲了我自己的理智。

FINAL工作液

(function ($) { 

     var config = {}; 

     $(document).ready(function() { 

      $(window).on("resize", function() { 
       resizeWindow(); 
      }).trigger("resize"); 

      var $container = $('.email-signup__wrap'), 
       $cHeight = $('.email-signup').outerHeight(); 

      $(document).on({ 
       mouseenter: function() { 
        TweenMax.to($container, .4, { 
         ease: Power2.easeOut, 
         css:{ 
          overflow: 'visible', 
          position: 'absolute', 
          top: -$cHeight 
         } 
        }); 
       }, 
       mouseleave: function() { 
        TweenMax.to($container, .4, { 
         ease: Power2.easeOut, 
         css:{ 
          position: 'relative', 
          top: 0 
         }, 
         onComplete: hide 
        }); 

        function hide(){ 
         $container.css('overflow', 'hidden'); 
        } 

        $("div.mce_inline_error").remove(); 
       } 
      }, ".dosomething .email-signup__wrap"); 
     }); 

     function resizeWindow() { 
      config.wWidth = $(window).width(); 

      if (config.wWidth > 768) { 
       $('body').addClass('dosomething'); 
      } else { 
       $('body').removeClass('dosomething'); 
      } 
     } 

    })(jQuery); 
+0

刪除'$(文件)。就緒(函數(){''因爲(函數($){'是一樣的。 –

+4

@ C-鏈接不,它不是。' $(函數(){'將是相同的。 – JJJ

+1

(函數($){即在javascript中關閉 –

回答

2

使用event delegation jQuery中。你動態地添加類.dosomething

$('body').on('mouseenter mouseleave', '.dosomething .email-signup__wrap' , function(ev) { 
+0

你能解釋這一點讓我明白請Sudharsan –

+0

http://davidwalsh.name/event-delegate –

+0

你添加並移除了doSomething類,並且你還爲同一個類編寫了mouseenter mouseleave的事件,所以腳本檢查所有的時間類是否存在,但是事件委託使它移動到文檔中去搜索類並得到它 –

相關問題