gpt4 book ai didi

javascript - 仅在移动设备上加载部分内容

转载 作者:行者123 更新时间:2023-11-28 10:38:07 25 4
gpt4 key购买 nike

使用的技术:Bootstrap 3.1.1、jQuery 1.11

目标:我有一个部分,我只想在移动设备上加载它的内容。

如果我只使用 Bootstrap 的 visible-sm 和 hidden-lg 类,内容仍会加载到更大的设备/尺寸上,从而产生开销。

我的部分代码如下。我只想在移动设备上加载和显示它的内容。我知道可见类和隐藏类显示和隐藏,但我只想在移动设备上加载它——这是关键。没有开销。

<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>

最佳答案

不确定为什么这会被否决。一个简单的 jQuery 插件就可以做到这一点:

(function(global, $) {

if (!global.matchMedia) {
throw new Error("This browser does not support matchMedia()");
}

$.fn.lazyLoad = function(overrides) {
var options = $.extend({}, $.fn.lazyLoad.defaults, overrides || {});

this.find("[data-lazyload-url]").each(function() {
var url = this.getAttribute("data-lazyload-url"),
media = this.getAttribute("data-lazyload-media"),
$target = $(this).addClass(options.loadingClass);

if (!media || global.matchMedia(media)) {
$.ajax(url)
.done(function(data) {
$target.html(data);
})
.fail(function(xhr, status, error) {
$target.html("Error: " + xhr.status);
})
.always(function() {
$target.removeClass(options.loadingClass);
});
}
});
};

$.fn.lazyLoad.defaults = {
loadingClass: "loading"
};

})(this, jQuery);

并使用它:

$(document).lazyLoad();

一些示例 HTML:

<body>
<div data-lazyload-url="/foo/bar">
Load for every browser
</div>
<div data-lazyload-url="/baz" data-lazyload-media="screen and max-width: 500px">
Load for 500px and smaller viewports
</div>
</body>

这使用了最新版本的 jQuery,但我认为您可能需要进行的唯一更改涉及对 $.ajax 的调用。

关于javascript - 仅在移动设备上加载部分内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23251273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com