简体   繁体   English

如何获取JavaScript在桌面而不是移动设备上运行功能

[英]How to get javascript to run a function on desktop but not on mobile

I'm using canvas for this. 我为此使用画布。 I have a picture and when a user clicks one of the elements I've specified, it highlights the part of the image that's described by that element. 我有一幅图片,当用户单击我指定的元素之一时,它将突出显示该元素描述的图像部分。

        $(function() { 
        $("#monsterTop").click(function() {
            $("#container").css("position", "relative");
            $("#highlight").css("position", "absolute")
                            .css("width", "413px")
                            .css("height", "32px")
                            .css ("top", "0px")
                            .css("left", "0px")
                            .css("background", "rgba(255, 0, 0, 0.4)"); 
               });
          });

And here's the relevant html code 这是相关的html代码

   <div id="container">
    <img src="../pictures/img/default.png" alt="Default page layout" class="adcanvas"/>
    <div id="highlight"></div>
</div>

I've got that working great, but the picture is too big for mobile. 我的工作效果很好,但是对于移动设备而言,图片太大了。 So I put this in my CSS: 所以我把它放在我的CSS中:

@media(max-width: 826px){
    .adpics{
    display:inline-block;
    width: 100%;
    max-width: 500px;
    }
.adinfo{
    display:inline-block;
    width:100%;
    }
    .adcanvas{
        display:none;
    }
}

That makes the canvas (id=adcanvas) disappear in the mobile and shows thumbnails of the larger image instead (id=adpics). 这样一来,画布(id = adcanvas)在移动设备中消失了,而是显示了较大图像的缩略图(id = adpics)。

The problem is that the highlighting from the javascript still shows up. 问题是javascript的高亮显示仍然出现。 I've tried an IF ELSE statement telling it to run only if the window is >826, but it just causes the entire script to not run on any size screen. 我已经尝试过IF ELSE语句,告诉它仅在窗口> 826时才运行,但是这只会导致整个脚本无法在任何尺寸的屏幕上运行。

How do I tell it not to run #highlight in mobile? 如何告诉我不要在移动设备上运行#highlight?

Link to the page as it runs so far. 链接到目前为止运行的页面。

In CSS file: 在CSS文件中:

/* Smartphones ----------- */
@media only screen and (max-width: 760px) {
  #element { display: none; }
}

In jQuery/Javascript file: 在jQuery / Javascript文件中:

$( document ).ready(function() {      
    var is_mobile = false;

    if( $('#element').css('display')=='none') {
        is_mobile = true;       
    }


    if (is_mobile == true) {
        //Conditional script here
    }
 });

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

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