2017-02-08 28 views
-1

我真的很抱歉我知道同一个问题有很多类似的主题。除了我感到困惑,因为我注意到没有禁用函数的通用代码。我认为与JS是复杂的(我不知道)。但这是情况。我在互联网上的某个地方发现了一个jquery代码,我想通过元素或wordpress使用它来使背景图像也滚动(视差) 一切都很完美。唯一的问题是移动设备,效果是腐败的,看起来很糟糕,我想禁用它。这里是代码:在移动设备上禁用Jquery视差

function jquery_parallax() { 

    ?> 
    <script language="JavaScript" type="text/javascript"> 


;(function($) { 

    'use strict' 

    var testMobile; 
    var isMobile = { 
     Android: function() { 
      return navigator.userAgent.match(/Android/i); 
     }, 
     BlackBerry: function() { 
      return navigator.userAgent.match(/BlackBerry/i); 
     }, 
     iOS: function() { 
      return navigator.userAgent.match(/iPhone|iPad|iPod/i); 
     }, 
     Opera: function() { 
      return navigator.userAgent.match(/Opera Mini/i); 
     }, 
     Windows: function() { 
      return navigator.userAgent.match(/IEMobile/i); 
     }, 
     any: function() { 
      return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows()); 
     } 
    }; 

    var parallax = function() { 
     testMobile = isMobile.any(); 
     if (testMobile == null) { 
      $(".parallax").parallax("50%", 0.3); 
     } 
    }; 



    // Dom Ready 
    $(function() { 
     parallax(); 
    }); 
})(jQuery); 

/* 
jQuery Parallax 1.1.3 
Author: Ian Lunn 
Plugin URL: http://www.ianlunn.co.uk/plugins/jquery-parallax/ 

Dual licensed under the MIT and GPL licenses: 
http://www.opensource.org/licenses/mit-license.php 
http://www.gnu.org/licenses/gpl.html 
*/ 
!function(n){var t=n(window),e=t.height();t.resize(function(){e=t.height()}),n.fn.parallax=function(o,r,i){function u(){var i=t.scrollTop();l.each(function(t,u){var l=n(u),f=l.offset().top,s=a(l);i>f+s||f>i+e||l.css("backgroundPosition",o+" "+Math.round((l.data("firstTop")-i)*r)+"px")})}var a,l=n(this);l.each(function(t,e){$element=n(e),$element.data("firstTop",$element.offset().top)}),a=i?function(n){return n.outerHeight(!0)}:function(n){return n.height()},(arguments.length<1||null===o)&&(o="50%"),(arguments.length<2||null===r)&&(r=.1),(arguments.length<3||null===i)&&(i=!0),t.bind("scroll",u).resize(u),u()}}(jQuery); 

    </script> 
    <?php 

} 


if (!(is_admin())) { 
wp_enqueue_script('jquery'); 
add_action('wp_head', 'jquery_parallax'); 
} 

谢谢

回答

0
if (!testMobile) { 
     $(".parallax").parallax("50%", 0.3); 
} 

你不需要核对空,你要检查,如果这不是真的(!)。

顺便说一句“禁用功能似乎很复杂”:

yourfunctiontodisable=()=>console.log("disabled function called"); 
+0

事实上,他的测试功能,并返回'null'。它*应该*只返回true/false。 – Blazemonger

+0

感谢您的快速回复。 我试图用“false”和“true”替换null,reslut是现在被禁用的视差。我只想在移动设备上禁用它。 再次感谢您的先生! – AgadirShop