avaScript 系列四:第2課

vanilla-lazyload 套件

by applelily

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript 系列四:第2課 ── vanilla-lazyload 套件</title>
    <link rel="stylesheet" href="./02_js_vanilla-lazyload.css">


</head>
<body>
 
   <!-- lazy load -->
   <div class="wrap">
    <div class="photos">
        <ul>
            <li>
                <!-- <a href="#"></a>
                <img
                alt="Lazy below"
                class="lazy"
                data-src="https://picsum.photos/1200/800/?random=1"
                /> -->
                <a href="#"></a>
                <img
                alt="Lazy below"
                class="lazy"
                data-src="https://images.unsplash.com/photo-1547494784-d143dea885b4?w=800&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MjZ8fG1jZG9uYWxkc3xlbnwwfHwwfHx8MA%3D%3D"
                />
            </li>
            <li>
                <a href="#"></a>
                <img
            alt="Lazy below"
            class="lazy"
            data-src="https://images.unsplash.com/photo-1628430154036-db08593fc659?w=800&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTJ8fG1jZG9uYWxkc3xlbnwwfHwwfHx8MA%3D%3D"
            />
            </li>
            <li>
                <a href="#"></a>
                <img
            alt="Lazy below"
            class="lazy"
            data-src="https://images.unsplash.com/photo-1682536221297-9f88f06c6cc9?w=800&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxjb2xsZWN0aW9uLXBhZ2V8OXxoMWFPMDZIQlRpVXx8ZW58MHx8fHx8"
            />
            </li>
            <li>
                <a href="#"></a>
                <img
            alt="Lazy below"
            class="lazy"
            data-src="https://media.istockphoto.com/id/1419323066/photo/avocado-cheeseburger.jpg?s=612x612&w=0&k=20&c=21Y2xPj36bAdZSOdSRYRAJCf737MB1HezFm265q4WSM="
            />
            </li>
 ...

CSS

*{
    margin: 0;
    padding: 0;
    list-style: none;
}


.wrap{
    box-sizing: border-box;
    margin: 20px auto;
}
.photos img{
    width: 100%;
    vertical-align: bottom;
}
li {
    background-color: #fff;
    padding: 5px;
    margin-bottom: 30px;
    position: relative;
  }

  li:after {
    content: '';
    background-color: rgba(0, 0, 0, 0.4);
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    position: absolute;
    transition: all 0.5s;
  }

  li:hover::after {
    background-color: rgba(0, 0, 0, 0);
    cursor: pointer;
    
  }

JavaScript

var lazyLoadInstance = new LazyLoad({
    // Your custom settings go here
  });