简体   繁体   English

通过使用针对IE和Safari的浏览器检测来关闭Stellar.js

[英]Turning off Stellar.js on by using browser detection for IE and Safari

I'm still learning jQuery, and I'm trying to figure out why this script isn't working. 我仍在学习jQuery,并且试图弄清为什么此脚本无法正常工作。

The goal is to turn off Stellar.js parallax for mobile, which I've done by detecting a specific CSS class. 目标是关闭用于移动设备的Stellar.js视差,这是通过检测特定的CSS类来完成的。 I'm also trying to turn it off on Safari and IE because of jumpy performance when using a mousewheel to scroll. 我也试图在Safari和IE上将其关闭,因为使用鼠标滚轮滚动时的性能令人y舌。 Any assistance troubleshooting, since I know the code is syntatically valid, would be awesome. 因为我知道代码在语法上是有效的,所以任何辅助的故障排除都很棒。

(It's wrapped in the "jQuery(document).ready(function($)" to function well in WordPress.) (它包装在“ 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,
       });
    }

});

Update: I cleaned this up, but now I'm getting an error when I inspect that isFirefox, isChrome, etc., are not defined. 更新:我清理了此问题,但是现在当我检查未定义isFirefox,isChrome等时,出现了错误。 Is that because I'm calling the variables incorrectly? 那是因为我错误地调用了变量吗?

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,
       });
    });
   } 
   } 
});  

This resolves it. 这样就解决了。 Turns out I had to do it within the first function and I needed to call the variables within the screen size check. 原来,我必须在第一个函数中执行此操作,并且需要在屏幕尺寸检查中调用变量。 I hope this is helpful to someone else! 我希望这对其他人有帮助!

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,
    });
});
} 
} 
});  

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

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