2016-06-18 3 views
1

私はtyped.jsを使用しており、モバイルでは動作させたくありません。私のコードは次のとおりです。画面サイズが満たされている場合にのみ動作するようにスクリプトを調整する必要があります

jQuery(document).ready(function ($) { 
    // Typed Span Element 
    $(window).on('resize', function () { 
     var win = $(this); 
     if (win.width() < 765) { 
      $(".typed-element").typed({ 
       strings: [ "Aim^1500" ], 
       typeSpeed: 200, 
       loop: false, 
       showCursor: true, 
       cursorChar: "|", 
       // time before typing starts 
       startDelay: 0, 
       // backspacing speed 
       backSpeed: 0, 
       // time before backspacing 
       backDelay: 500 
      }); 
     } else { 
      $(".typed-element").typed({ 
       strings: [ "Aim^1500", "Game^1500", "Internet^1500" ], 
       typeSpeed: 200, 
       loop: true, 
       showCursor: true, 
       cursorChar: "|", 
       // time before typing starts 
       startDelay: 0, 
       // backspacing speed 
       backSpeed: 0, 
       // time before backspacing 
       backDelay: 500 
      }); 
     } 
    }); 

}); 

さらに完全にスクリプトを停止する方法があれば、さらに理想的です。私の回避策は、文字列を調整して1つの単語だけを表示することです。

答えて

0

モバイルでこれを読み込んでいる場合は、JavaScriptが開始される前にwindow.width()がわかります。

jQuery(document).ready(function ($) { 
    // This is a mobile and there is no reason to activate the typed element. 
    // return stops the processing of the rest of the code. 
    if($(window).width() < 765) return 

    //This should NEVER be hit on a mobile and will be just fine on everything else. 
    // Typed Span Element 
    $(window).on('resize', function () { 
     var win = $(this); 
     if (win.width() < 765) { 
      $(".typed-element").typed({ 
       strings: [ "Aim^1500" ], 
       typeSpeed: 200, 
       loop: false, 
       showCursor: true, 
       cursorChar: "|", 
       // time before typing starts 
       startDelay: 0, 
       // backspacing speed 
       backSpeed: 0, 
       // time before backspacing 
       backDelay: 500 
      }); 
     } else { 
      $(".typed-element").typed({ 
       strings: [ "Aim^1500", "Game^1500", "Internet^1500" ], 
       typeSpeed: 200, 
       loop: true, 
       showCursor: true, 
       cursorChar: "|", 
       // time before typing starts 
       startDelay: 0, 
       // backspacing speed 
       backSpeed: 0, 
       // time before backspacing 
       backDelay: 500 
      }); 
     } 
    }); 

}); 
0

これらはwindow.screen.availWidthが765未満の場合、それはモバイルだブラウザの高さと幅

window.screen.availHeight 
window.screen.availWidth 

ための変数です。

if(window.screen.availWidth < 765){ 
    //your code here 
} 
関連する問題