LazyLaoding CSS背景(不是HTML< img>标签) [英] LazyLaoding CSS background (not HTML <img> tags)

查看:50
本文介绍了LazyLaoding CSS背景(不是HTML< img>标签)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法弄清楚了如何在我的网站上延迟加载图片,并通过< img> 标签将图片调用为HTML.我在GitHub上使用了此软件包来完成此操作.但是我在完成时遇到了麻烦与CSS background:的情况相同.

I've managed to figure out how to lazyload images on my website where the images are called into the HTML via <img> tags. I used this package on GitHub to accomplish this. However I'm having trouble acomplishing the same thing with CSS background:'s.

-有关此问题的一些信息:

我昨晚创建了一个图像菜单栏,该菜单栏根据浏览器屏幕(提供链接)来更改图像大小/位置.此图像菜单栏中的所有图像都是通过CSS中的 background:调用的(而不是通过< img> 标记).

I created an image menu bar last night that changes image size/position based on the browsers screen (Link provided). All the images in this image menu bar are called in via background: within the CSS (not through an <img> tag).

-现在是我的问题:

我如何在我已经开始的项目.在我的网站上,按照以下说明进行操作后,我只需将< img src ="> 更改为< img data-src =" (在HTML中)点击上方的Github链接,图像将在屏幕上显示时加载.

How am i able to lazyload the background: images within the css of this project I've started. On my website I would just change <img src=""> to <img data-src="" (within the HTML) after following the instructions from the Github link above, and images will load as they become visible on the screen.

-执行此操作的原因:

这纯粹是为了加快网站速度,并避免一次加载所有图像.确保该加载时间不会浪费任何其他提示,或者解决此问题的任何其他可能方式,将不胜感激.

This is purely to speed up the website and to avoid loading all images at once. Any other tips to ensure this does not take a toll on loading time, or any other possible ways around this issue would be appreciated.

-注意:对于该项目,我使用了 background:而不是 background-image:.

-Note: I've used background: and not background-image: for this project.

如果您错过了我项目的链接: https://jsfiddle.net/Shololom/wbgn210y/

推荐答案

来自 Github您提供的项目:

何时使用:您的图像被设置为CSS背景图像,而不是真实的img,但是您仍然希望延迟加载它们.

When to use: your images are set as CSS background images instead of real img, but you still want to lazily load them.

<div class="lazy" data-src="../img/44721746JJ_15_a.jpg"></div>

Javascript

var myLazyLoad = new LazyLoad({
    elements_selector: ".lazy"
});

就是这样.每当由elements_selector选择的元素不是img或iframe时,LazyLoad会将在data-src属性中找到的图像放在该元素的背景图像中.

That's it. Whenever the element selected by elements_selector is not an img or an iframe, LazyLoad puts the image found in the data-src attribute in the background-image of the element.

// Get products
var product1 = document.getElementById('product1');
var product2 = document.getElementById('product2');
var product3 = document.getElementById('product3');
var product4 = document.getElementById('product4');
var product5 = document.getElementById('product5');

// Create Lazy loader
var myLazyLoad = new LazyLoad({
    elements_selector: ".lazy"
});

// Load images in a responsive way
function loadImgs() {
	console.log('Loading images ...');
  	var src1;
    var src2;
    var src3;
    var src4;
    var src5;    
	if(window.matchMedia("only screen and (max-width:700px)").matches) {
      /* The viewport is lesser than 700 pixels wide */
  	src1 = product1.getAttribute('data-src-small');
    src2 = product2.getAttribute('data-src-small');
    src3 = product3.getAttribute('data-src-small');
    src4 = product4.getAttribute('data-src-small');
    src5 = product5.getAttribute('data-src-small');      
   
  } else {
  	src1 = product1.getAttribute('data-src-large');
    src2 = product2.getAttribute('data-src-large');
    src3 = product3.getAttribute('data-src-large');
    src4 = product4.getAttribute('data-src-large');
    src5 = product5.getAttribute('data-src-large');   
  } 

  // Set the data-src for lazy loader
  product1.setAttribute('data-src', src1);
  product2.setAttribute('data-src', src2);
  product3.setAttribute('data-src', src3);
  product4.setAttribute('data-src', src4);
  product5.setAttribute('data-src', src5); 
    
  // Tell lazy loader that the data should be re-processed
  product1.removeAttribute('data-was-processed');
  product2.removeAttribute('data-was-processed');
  product3.removeAttribute('data-was-processed');
  product4.removeAttribute('data-was-processed');
  product5.removeAttribute('data-was-processed');   
  
  // Tell lazy loader to update
  myLazyLoad.update();
}

// Load images first time
loadImgs();

// Reload images when window is resized
var lastWindowSize = window.innerWidth;
window.onresize = function(event) {
	var currentWindowSize = window.innerWidth; 
	if((lastWindowSize <= 700 && currentWindowSize > 700) || (lastWindowSize > 700 && currentWindowSize <= 700)) {
  	loadImgs();
  }
	
  lastWindowSize = currentWindowSize;
};

.top-header-settings {
  font-family: inherit;
  font-size: 18px;
  font-weight: 400;
  text-transform: uppercase;
  letter-spacing: .1em;
  margin: 0 0 45px;
  padding: 45px 0 0;
  color: #524949;
  border-top: 1px solid #ddd;
}

.top-menu-wrap {
  margin: 0 auto;
  position: relative;
  ;
  background-color: #343434;
}

.top-menu-inner {
  max-width: 1200px;
  height: 260px;
  /*/background-color:#343434;/*/
  margin: 0 auto;
}

.top-menu-innerr {
  max-width: 1161px;
  height: 200px;
  margin: 0 auto;
  padding-top: 20px;
}

.top-menu-button {
  width: 220px;
  height: 220px;
  display: inline-block;
  margin: 5px;
  text-align: center;
  font-size: 16px;
  opacity: .6;
  transition: 0.3s;
}

.top-menu-button:hover {
  opacity: 1
}

@font-face {
  font-family: 'roboto';
  src: url(https://fonts.googleapis.com/css?family=Roboto);
}

.top-menu-text {
  width: 125px;
  height: 30px;
  /*/background-color:red;/*/
  margin: 150px auto;
  text-align: center;
  color: white;
  font-family: 'roboto';
  font-size: 20px;
  background: #343434;
  -khtml-opacity: .50;
  -moz-opacity: .50;
  -ms-filter: "alpha(opacity=50)";
  filter: alpha(opacity=50);
  filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0.5);
  opacity: .60;
  border-style: solid;
  border-width: 1px;
  line-height: 25px;
}

.coming-soon-top {
  width: 75px;
  height: 20px;
  font-size: 10px;
  text-align: center;
  color: white;
  position: absolute;
  font-family: 'roboto';
  background: #c78f8f;
}

@media only screen and (max-width:1200px) {
  .top-menu-wrap {
    margin: 0 auto;
    position: relative;
    background-color: #343434;
  }
  .top-menu-inner {
    max-width: 1200px;
    min-height: 340px;
    /*/ background-color:#343434;/*/
    margin: 0 auto;
  }
  .top-menu-innerr {
    max-width: 80%;
    min-height: 200px;
    width: 80vw;
    margin: 0 auto;
    /*/background:red;/*/
  }
  .top-menu-button {
    width: 80vw;
    height: 80vw;
    max-width: 1080px;
    max-height: 1080px;
    background-repeat: no-repeat;
    background-size: contain;
    background-position: center;
    float: left;
    margin: 1px auto;
    text-align: center;
    font-size: 16px;
    margin: 4px 2px;
    opacity: 1;
    transition: 0.3s;
    color: white;
  }
  .top-menu-button:hover {
    opacity: .6
  }
  #product2 {
    /*background: url("http://via.placeholder.com/1080x1080");*/
    /*/background:#c77f7f;/*/
    width: 80vw;
    height: 80vw;
    max-width: 1080px;
    max-height: 1080px;
    background-repeat: no-repeat;
    background-size: contain;
    background-position: center;
  }
  #product1 {
    /*background: url("http://via.placeholder.com/1080x1080");*/
    /*/background:#c77f7f;/*/
    width: 80vw;
    height: 80vw;
    max-width: 1080px;
    max-height: 1080px;
    background-repeat: no-repeat;
    background-size: contain;
    background-position: center;
  }
  #product3 {
    /*background: url("http://via.placeholder.com/1080x1080");*/
    /*/background:#c77f7f;/*/
    width: 80vw;
    height: 80vw;
    max-width: 1080px;
    max-height: 1080px;
    background-repeat: no-repeat;
    background-size: contain;
    background-position: center;
  }
  #product4 {
    /*background: url("http://via.placeholder.com/1080x1080");*/
    /*/background:#c77f7f;/*/
    width: 80vw;
    height: 80vw;
    max-width: 1080px;
    max-height: 1080px;
    background-repeat: no-repeat;
    background-size: contain;
    background-position: center;
  }
  #product5 {
    /*background: url("http://via.placeholder.com/1080x1080");*/
    /*/background:#c77f7f;/*/
    width: 80vw;
    height: 80vw;
    max-width: 1080px;
    max-height: 1080px;
    background-repeat: no-repeat;
    background-size: contain;
    background-position: center;
  }
  .top-menu-text {
    /*/width:125px;/*/
    /*/height:30px;/*/
    width: 30vw;
    height: 10vw;
    /*/background-color:red;/*/
    margin: 150px auto;
    text-align: center;
    color: white;
    font-size: 5.9vw;
    line-height: 9vw;
  }
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/vanilla-lazyload/8.7.1/lazyload.min.js"></script>
<div class="top-menu-warp">
  <div class="top-menu-inner">
    <div class="top-menu-innerr">


      <a href="#">
        <div id="product1" class="top-menu-button lazy" 
          data-src-small="http://via.placeholder.com/220x220"
          data-src-large="http://via.placeholder.com/1080x1080">

          <div class="coming-soon-top">
            Most Popular
          </div>

          <div class="top-menu-text">
            Text
          </div>
        </div>
      </a>

      <a href="#">
        <div id="product2" class="top-menu-button lazy" 
          data-src-small="http://via.placeholder.com/220x220"
          data-src-large="http://via.placeholder.com/1080x1080">
          <div class="top-menu-text">
            Text
          </div>
        </div>
      </a>

      <a href="#">
        <div id="product3" class="top-menu-button lazy" 
          data-src-small="http://via.placeholder.com/220x220"
          data-src-large="http://via.placeholder.com/1080x1080">
          <div class="top-menu-text">
            Text
          </div>
        </div>
      </a>

      <a href="#">
        <div id="product4" class="top-menu-button lazy" 
          data-src-small="http://via.placeholder.com/220x220"
          data-src-large="http://via.placeholder.com/1080x1080">
          <div class="top-menu-text">
            Text
          </div>

        </div>
      </a>

       <a href="#">
        <div id="product5" class="top-menu-button lazy" 
            data-src-small="http://via.placeholder.com/220x220"
            data-src-large="http://via.placeholder.com/1080x1080">

          <div class="coming-soon-top">
            Coming soon
          </div>

          <div class="top-menu-text">
            Text
          </div>
        </div>
      </a>
    </div>

  </div>

一个jsfiddle

这篇关于LazyLaoding CSS背景(不是HTML&lt; img&gt;标签)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆