简体   繁体   English

仅在移动设备上显示div

[英]Displaying div only on mobile devices

I have a div that I'd like to only load on mobile devices - and mobile browser sizes. 我有一个仅想在移动设备上加载的div-和移动浏览器大小。

I am using Bootstrap 3.1.1, but using the visible & hidden classes still loads the content in the background even if it's hidden. 我正在使用Bootstrap 3.1.1,但是使用可见和隐藏类仍然将内容加载到后台,即使它是隐藏的。

So, anyway to do this easily? 那么,无论如何轻松做到这一点? Where I can just paste my HTML into a section of code and it just works? 在哪里我可以将HTML粘贴到一段代码中,并且可以正常工作?

Edit 编辑

I only want to load this entire section of code on mobile devices & mobile browser sizes (if I resize my desktop browser). 我只想在移动设备和移动浏览器大小上加载整个代码部分(如果我调整桌面浏览器的大小)。 Like I said, I am using Bootstrap 3.1.1, but the code still loads in the background, defeating the point of what I'm trying to do; 就像我说的那样,我正在使用Bootstrap 3.1.1,但是代码仍然在后台加载,这超出了我要执行的操作; load lighter content on mobile, load heavier content on desktop. 在移动设备上加载较轻的内容,在台式机上加载较重的内容。

With the code below, it gets loaded in the background anyways, creating overhead. 使用下面的代码,无论如何它都会在后台加载,从而产生开销。 I need a way to insert that entire section of code into a page only if the user is on a mobile device or their browser is a mobile width. 我仅需要在用户使用移动设备或浏览器为移动宽度时才需要将整个代码段插入页面。

I want the code itself to only even be included if on mobile device or mobile browser size. 我希望代码本身甚至仅在移动设备或移动浏览器大小的情况下被包括在内。

All Bootstrap does is hide the content from the front-end via display:none; Bootstrap所做的全部工作就是通过display:none;从前端隐藏内容。 it still gets loaded when someone visits the page. 当有人访问该页面时,它仍然会加载。 I need to not even load it until needed. 我什至不需要加载它,直到需要为止。

<section id="category-icons" class="row visible-xs visible-sm hidden-md hidden-lg">
    <div class="container White_BG">
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Hand Tools" target="_blank"><img class="img-responsive" src="/images/art/Handtools-cat-sprite.png"></a>         
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Field Production" target="_blank"><img class="img-responsive" src="/images/art/Fieldproduction-cat-sprite.png"></a>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Pruning Tools" target="_blank"><img class="img-responsive" src="/images/art/Pruning-cat-sprite.png"></a>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Material Handling" target="_blank"><img class="img-responsive" src="/images/art/materialhandling-cat-sprite.png"></a>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Safety Equipment" target="_blank"><img class="img-responsive" src="/images/art/safetyequipment-cat-sprite.png"></a>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Sprayers & Spreaders" target="_blank"><img class="img-responsive" src="/images/art/sprayers-spreaders-cat-sprite.png"></a>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Watering & Irrigation" target="_blank"><img class="img-responsive" src="/images/art/watering-cat-sprite.png"></a>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Marking & Tagging" target="_blank"><img class="img-responsive" src="/images/art/markingtagging-cat-sprite.png"></a>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Herbicides & Pesticides" target="_blank"><img class="img-responsive" src="/images/art/herbicides-cat-sprite.png"></a>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Greenhouse Production" target="_blank"><img class="img-responsive" src="/images/art/greenhouse-cat-sprite.png"></a>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Fertilizers & Treatments" target="_blank"><img class="img-responsive" src="/images/art/fertilizers-cat-sprite.png"></a>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Container Production" target="_blank"><img class="img-responsive" src="/images/art/containerproduction-cat-sprite.png"></a>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Pest Control" target="_blank"><img class="img-responsive" src="/images/art/pestcontrol-cat-sprite.png"></a>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Power Equipment" target="_blank"><img class="img-responsive" src="/images/art/powerequipment-cat-sprite.png"></a>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Work Wear" target="_blank"><img class="img-responsive" src="/images/art/workwear-cat-sprite.png"></a>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Turf & Landscape" target="_blank"><img class="img-responsive" src="/images/art/turf-landscape-cat-sprite.png"></a>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Arborist Equipment" target="_blank"><img class="img-responsive" src="/images/art/arborists-cat-sprite.png"></a>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Measuring & Surveying" target="_blank"><img class="img-responsive" src="/images/art/measuring-cat-sprite.png"></a>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Maintenance Supplies" target="_blank"><img class="img-responsive" src="/images/art/maintenance-cat-sprite.png"></a>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Snow Removal" target="_blank"><img class="img-responsive" src="/images/art/snowremoval-cat-sprite.png"></a>
        </div>
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
            <a href="/hand-tools/c/P01/" title="Used, Scratch, Dented Items" target="_blank"><img class="img-responsive" src="/images/art/useditems-cat-sprite.png"></a>
        </div>
    </div>
</section>

I am not sure if bootstrap detects if it is a moivil device, what I am sure it does, it detects the screen resolution of your device, accoirding to your @media config 我不确定bootstrap是否检测到它是moivil设备,我确定它会做什么,它会根据您的@media配置检测到您设备的屏幕分辨率

basically you have 4 choices 基本上你有4个选择

visible-lg large screens, visible-md medium screens, visible-sm small screens, visible-xs extra small screens visible-lg大屏幕, visible-md中型屏幕, visible-sm小屏幕, visible-xs特小屏幕

so if you want this div to be visible just in small devices you should add visible-xs class as follows: 因此,如果您希望此div仅在小型设备中可见,则应添加visible-xs类,如下所示:

<div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 visible-xs">
        <a href="/hand-tools/c/P01/" title="Hand Tools" target="_blank"><img class="img-responsive" src="/images/art/Handtools-cat-sprite.png"></a>         
    </div>

http://getbootstrap.com/css/#responsive-utilities http://getbootstrap.com/css/#responsive-utilities

You have quite a few images, so this isn't exactly optimal, but one solution is to make use of background-image and media queries. 您有很多图像,因此这并不是最佳选择,但是一种解决方案是利用background-image和媒体查询。 If the device doesn't satisfy the constraint on width , the image won't be downloaded and hence not rendered. 如果设备不满足width限制,则不会下载图像,因此不会渲染图像。 To make it work you'll need to have some CSS such as 要使其正常工作,您需要具有一些CSS,例如

.col img {
    min-width: 100%;
    min-height: 100%;
}

Where the surrounding div has a .col class and explicit width and height (this isn't required, you can tweak the image size attributes or the div s). 周围的div具有.col类以及显式的widthheight (这不是必需的,您可以调整图像大小属性或div )。 Then in your css add 然后在你的CSS中添加

/* assuming mobile is < 768px  */
@media screen and (min-width: 768px) {
    .no-mobile-img-1 {
        background: url('../path/to/my/image.jpeg');
    }
}

You could wrap it up in a SASS function to iterate through 1 - max images and put the appropriate image for the background: 您可以将其包装在SASS函数中以迭代1 - max images并为背景放置适当的图像:

@media screen and (min-width: 768px) {
    $images = banana, monkey, donkey;
    @for $i from 1 through 3 {
        $ith: nth($images, $i);
        .no-mobile-img-#{$i} {
            background: url("#{$ith}");
        }
    }
}

Then your html 然后你的HTML

<div class="col">
    <img src="" class="no-mobile-img-1" alt="some text">
</div>

Fiddle : http://jsfiddle.net/kYC8C/1/ 小提琴http : //jsfiddle.net/kYC8C/1/
Check the network tab at 768px , then resize the window and watch the image be downloaded in the network tab. 检查网络标签为768px ,然后调整窗口大小并观看图像在网络标签中的下载。

Alternate Solutions : 替代解决方案

  • Server side detection of user-agent/viewport that serves a mobile/desktop version of the page depending on the user's device/size. 服务器端对用户代理程序/视口的检测,该代理程序视页面的移动/桌面版本而定,具体取决于用户的设备/大小。
  • Rewrite rule to redirect to mobile/desktop versions of the site. 重写规则以重定向到网站的移动/桌面版本。
  • JavaScript to write out the div s/images based on the user's viewport; JavaScript根据用户的视口写出div /图像; lazyload, jQuery, etc. lazyload,jQuery等

I've been playing with JS cookies! 我一直在玩JS cookie!

This might be a possible answer..using cookies.. 这可能是一个可能的答案。

First you have this css block: 首先,您具有以下css块:

@media (max-width: 600px)
{
    .hideForMobile
    {
        display: none;
    }
}

Add the class .hideForMobile to the tags you want to hide it on the mobile phone. 将类.hideForMobile添加到要在手机上隐藏的标签中。 This will be the identifier for the javascript as well. 这也是javascript的标识符。

Now, after the document is ready, we will do the following checks: 现在,准备好文档后,我们将进行以下检查:

  • Is the cookie isMobile set? cookie是否设置为isMobile
    • If true, don't do anything. 如果为true,则什么也不做。
    • If false, we will retrieve the current display state of class .hideForMobile 如果为false,我们将检索类.hideForMobile的当前显示状态。
      • We will store it as a cookie for the server. 我们将其存储为服务器的cookie。
  • When the user requests/reloads the page, the server can now check if it's a mobile device using the cookie, which will return true (=mobile) or false (=not mobile). 当用户请求/重新加载页面时,服务器现在可以使用cookie检查它是否是移动设备,该cookie将返回true(=移动)或false(=非移动)。

JS: JS:

$(function()
{
    //    check if true.
    var isTruthy=document.cookie.indexOf('isMobile=true;')>-1? true : undefined,
    //    check if false.
        isFalsey=document.cookie.indexOf('isMobile=false;')>-1? true : undefined;
    //    We haven't set a cookie for the first time.
    //    DO IT.
    if (isTruthy==undefined && isFalsey==undefined)
    {
        //    Check if it's hidden.
        //    We will set the cookie depending on the display.
        var isMobile = $('.hideForMobile').first().css('display')=='none';
        //    store cookie for the server.
        document.cookie='isMobile=' + isMobile + ';';
    }
});

Is this what you are looking for? 这是你想要的? You only need a server-sided script that checks if the cookie is set, if so, you can prevent data to be sent if it is a mobile device. 您只需要一个服务器端脚本来检查cookie是否已设置,如果已设置,则可以阻止在移动设备上发送数据。

NOTE: There is probably a cleaner way to it, for example using pure server-sided code (or checking USER_AGENT). 注意:可能有更清洁的方法,例如使用纯服务器端代码(或检查USER_AGENT)。

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

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