代码之家  ›  专栏  ›  技术社区  ›  aronmoshe_m

关闭恒星。使用IE和Safari浏览器检测打开js

  •  0
  • aronmoshe_m  · 技术社区  · 8 年前

    我还在学习jQuery,我正在试图找出这个脚本为什么不起作用。

    目标是关闭Stellar。移动设备的js视差,我通过检测特定的CSS类来实现。我还试图在Safari和IE上关闭它,因为当使用鼠标滚轮滚动时,会表现出跳跃性。因为我知道代码在语法上是有效的,所以任何帮助故障排除都是很棒的。

    (它被包装在“jQuery(document).ready(function($)”中,以便在WordPress中正常工作。)

     jQuery(document).ready(function($) {   
       $(document).ready(function() {
       // run test on initial page load
       checkSize();
    
       // run test on resize of the window
       $(window).resize(checkSize);
       });
    
       //Function to the css rule
       function checkSize(){
       if ($(".parallax").css("background-attachment") == "inherit" ){
          $(function () {
          $.stellar({
            horizontalScrolling: false,
            responsive: true, 
            parallaxBackgrounds: false,
         });
       });
       }
       if ($(".parallax").css("background-attachment") == "fixed" ){
          $(function () {
          $.stellar({
            horizontalScrolling: false,
            responsive: true, 
            parallaxBackgrounds: true,
          });
       });
       } 
       }
    
       // Opera 8.0+
       var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
    
       // Firefox 1.0+
       var isFirefox = typeof InstallTrigger !== 'undefined';
    
       // Safari 3.0+ "[object HTMLElementConstructor]" 
       var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || safari.pushNotification);
    
       // Internet Explorer 6-11
       var isIE = /*@cc_on!@*/false || !!document.documentMode;
    
       // Edge 20+
       var isEdge = !isIE && !!window.StyleMedia;
    
       // Chrome 1+
       var isChrome = !!window.chrome && !!window.chrome.webstore;
    
       // Blink engine detection
       var isBlink = (isChrome || isOpera) && !!window.CSS;
    
       // check if Safari or IE and disable parallax
       if(!isSafari || !isIE)
       {
           $(function () {
           $.stellar({
            horizontalScrolling: false,
            responsive: true, 
            parallaxBackgrounds: false,
           });
        }
    
    });
    

    我清理了这个,但现在当我检查isFirefox、isChrome等没有定义时,我发现了一个错误。这是因为我错误地调用了变量吗?

    jQuery(document).ready(function($) {    
       $(document).ready(function() {
    
       // hella browser checks  
       // Opera 8.0+
       var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
    
       // Firefox 1.0+
       var isFirefox = typeof InstallTrigger !== 'undefined';
    
       // Safari 3.0+ "[object HTMLElementConstructor]" 
       var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || safari.pushNotification);
    
       // Internet Explorer 6-11
       var isIE = /*@cc_on!@*/false || !!document.documentMode;
    
       // Edge 20+
       var isEdge = !isIE && !!window.StyleMedia;
    
       // Chrome 1+
       var isChrome = !!window.chrome && !!window.chrome.webstore;
    
       // Blink engine detection
       var isBlink = (isChrome || isOpera) && !!window.CSS;
    
       // run test on initial page load
       checkSize();
    
       // run test on resize of the window
       $(window).resize(checkSize);
    });
    
       //Function to the css rule
       function checkSize(){
         if ($(".parallax").css("background-attachment") == "inherit" ){
           $(function () {
           $.stellar({
            horizontalScrolling: false,
            responsive: true, 
            parallaxBackgrounds: false,
           });
         });
        }
        if ( ($(".parallax").css("background-attachment") == "fixed" ) && (!isFirefox || !isChrome || !isBlink || !isOpera) ) {
           $(function () {
           $.stellar({
            horizontalScrolling: false,
            responsive: true, 
            parallaxBackgrounds: true,
           });
        });
       } 
       } 
    });  
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   aronmoshe_m    8 年前

    这就解决了问题。事实证明,我必须在第一个函数中完成,并且我需要在屏幕大小检查中调用变量。我希望这对其他人有帮助!

    jQuery(document).ready(function($) {    
    $(document).ready(function() {
    
    // run test on initial page load
    checkSize();
    
    // run test on resize of the window
    $(window).resize(checkSize);
    });
    
    // hella browser checks  
    // Opera 8.0+
    var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
    
    // Firefox 1.0+
    var isFirefox = typeof InstallTrigger !== 'undefined';
    
    // Safari 3.0+ "[object HTMLElementConstructor]" 
    var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || safari.pushNotification);
    
    // Internet Explorer 6-11
    var isIE = /*@cc_on!@*/false || !!document.documentMode;
    
    // Edge 20+
    var isEdge = !isIE && !!window.StyleMedia;
    
    // Chrome 1+
    var isChrome = !!window.chrome && !!window.chrome.webstore;
    
    // Blink engine detection
    var isBlink = (isChrome || isOpera) && !!window.CSS;
    
    //Function to the css rule
    function checkSize(){
    if ($(".parallax").css("background-attachment") == "inherit" ){
        $(function () {
        $.stellar({
            horizontalScrolling: false,
            responsive: true, 
            parallaxBackgrounds: false,
        });
    });
    }
    if ( ($(".parallax").css("background-attachment") == "fixed" ) && (isFirefox || isChrome || isBlink || isOpera) ) {
        $(function () {
        $.stellar({
            horizontalScrolling: false,
            responsive: true, 
            parallaxBackgrounds: true,
        });
    });
    } 
    } 
    });