简体   繁体   English

我如何才能使此脚本仅在特定屏幕尺寸以上执行?

[英]How can I make it so this script executes only above a certain screen size?

I'm using this script which I purchased from some website in German, so I can't understand the instructions. 我使用的是从德国某个网站购买的脚本,所以我听不懂说明书。 Basically, I want to make it so this is only triggered on screens above 768 pixels. 基本上,我想这样做,因此只能在768像素以上的屏幕上触发。 I've tried a few different things and can't seem to get it to work. 我尝试了几种不同的方法,但似乎无法使其正常工作。 I'm not very well versed in Javascript. 我不太熟悉Javascript。 If someone could show me how to do this, I would be very grateful. 如果有人可以告诉我该怎么做,我将不胜感激。

  $(document).ready(function() {
    //Build Bubble Machines with the Bubble Engine ------------------------
    var SoapBubbleMachineNumber1 = $('fn').BubbleEngine({
      particleSizeMin:            0,
      particleSizeMax:            60,
      particleSourceX:            0,
      particleSourceY:            500,
      particleAnimationDuration:  5000,
      particleDirection:          'right',
      particleAnimationDuration:  6000,
      particleAnimationVariance:  2000,
      particleScatteringX:        500,
      particleScatteringY:        300,
      gravity:                    -100
    });
    //Start Bubble Machine 1 ---------------------------------------------
    SoapBubbleMachineNumber1.addBubbles(50);
  });
var w = window,
        d = document,
        e = d.documentElement,
        g = d.getElementsByTagName('body')[0],
        x = w.innerWidth || e.clientWidth || g.clientWidth;

if(x > 768){
   //your code here.
}

The first part is a pretty robust function to find the window width. 第一部分是一个相当健壮的函数,用于查找窗口宽度。

What you would then do is wrap your code shown in a function and call the function within the //your code here part 然后,您要做的就是将您的代码包装在一个函数中,并在//your code here部分中调用该函数

first google translator is your friend ;) If nothing works ask a german like me ;) Anyhow : 首先google翻译是你的朋友;)如果没有效果的话,请问我这样的德国人;)

 function executeme(minWidth,minHeight){  
      var width = window.innerWidth|| document.documentElement.clientWidth|| 
      Document.body.clientWidth;

      var height = window.innerHeight || document.documentElement.clientHeight || 
      document.body.clientHeight;

      if (width < minWidth){return false;}
      if (height < minHeight){return false;}
      return true;
 }

this will work on the browser window 这将在浏览器窗口中起作用

If you want to know how much space is left in a div for example 例如,如果您想知道div中还有多少空间

    function getGeometry(id){

         try{
         var element=document.getElementById()  
         } catch (ex){
             return false;
         }
         var rect = element.getBoundingClientRect(); 
         // uncomment to see the whole show :)
         // console.log(rect); 
         return rect;
    }

    function runOnElementGeometry(id,minWidth,minHeight){
         try{
         var element=document.getElementById()  
         } catch (ex){
             return false;
         }
         var rect = element.getBoundingClientRect(); 

         if (rect.width < minWidth){return false;}
         if (rect.height < minHeight){return false;}
         return true;
    }

if you need also the position of the element on the page you need to add the scroll positions (not needed for the width and height) 如果还需要元素在页面上的位置,则需要添加滚动位置(宽度和高度不需要)

      var x = window.scrollX
      var y = window.scrollY

Now you can check whats going on and either start your bubbeles or even not :) 现在,您可以检查发生了什么,然后启动泡泡,甚至不启动:)

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

相关问题 使脚本仅适用于特定的屏幕尺寸 - Make it so that a script only works at a certain screen size 我可以让它只在某个变量为真时才执行点击事件吗? - Can I make it so a click event only executes if a certain variable is true? 更改屏幕大小时如何使图像位于文本上方 - How can I make the image above the text when changing screen size 如何制作一个总是先执行到最后执行的脚本? - How can I make a script that always executes first to execute last? 仅当屏幕大于特定大小时才加载nivo型滑块 - Load nivo-type slider only if screen is above certain size 我在某些屏幕尺寸上禁用了某些jQuery脚本,但是如何在调整屏幕尺寸时检查它? - I Disabled certain jQuery script at certain screen size but how do I get it check when screen is resized? 如何制作一个按钮,使其只能在一定程度上被拖动? - How to make a button so that it can only be dragged to a certain extent? 如何仅在特定屏幕尺寸下将CSS类与脚本一起应用? - How can I apply a CSS class with my script only at certain screen sizes? 如何仅在特定屏幕尺寸下使HTML元素可单击 - How can I make an HTML element clickable only at certain screen sizes 如何放置底部菜单,使其在不同屏幕尺寸下都位于页面底部上方? - How can I position my bottom menu so that it stays above the bottom of the page across different screen sizes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM