Giter Site home page Giter Site logo

projecttemplate's Introduction

Gulp with TailwindCSS Starter Kit

Gulp with TailwindCSS Starter Kit - A repo which makes your development easier with predefined gulp tasks that help you to use tailwindcss with simple npm commands

Usage

  1. Install Dev Depedencies
npm install // or yarn install
  1. To start development and server for live preview
npm run dev // or yarn dev
  1. To generate minifed files for production server
npm run prod // or yarn prod

Configuration

To change the path of files and destination/build folder, edit options in config.js file

{
  config: {
      ...
      port: 9050 // browser preview port
  },
  paths: {
     root: "./",
     src: {
        base: "./src",
        css: "./src/css",
        js: "./src/js",
        img: "./src/img"
     },
     dist: {
         base: "./dist",
         css: "./dist/css",
         js: "./dist/js",
         img: "./dist/img"
     },
     build: {
         base: "./build",
         css: "./build/css",
         js: "./build/js",
         img: "./build/img"
     }
  }
  ...
}

Features

Image Lazy loading with thumbnail blurred preview

mixins.pug contains mixin +imgBlur for an image loading with thumbnail preview.

The technique was described here https://www.youtube.com/watch?v=hJ7Rg1821Q0.

To use the mixin you need to pass an object with variables as props for the mixin. It should contain either 1) thumb: or 2) suffix: key.

  1. thumb: key is a string that should be a full path to an image like '../media/image-blurred.jpg'

thumb: key should be used for +image mixin with simple config, aka simple <img> tag. src key is also a string that should be a full path to an image like '../media/image.jpg' src key can be ommited, but another option to give a path to an image should be used - via attributes. Example:

 +imgBlur({
    thumb: '../media/Hero-blurred.jpg',
    src: "../media/[email protected]",
    height: 1280
})(alt="Hero BG"  width="1920"  )

or

 +imgBlur({
    thumb: '../media/Hero-blurred.jpg',
})(alt="Hero BG"  width="1920" height="1280" src="../media/[email protected]")
  1. If an image has more than one resolution (responsive image) it has to have resolutions key as an array and suffix as a string. thumb option is mandatory while suffix is not. If suffix is ommited, default blurred suffix will be used.

Using +imgBlur for Responsive Image mixin with suffix key specified:

+imgBlur({
    suffix: 'thumbnail',
    name: 'Hero',
    ext: 'jpg',
    dir: mediaFolder,
    resolutions: [1366,1600, 1920],
    width: 1920,
    height: 1280
})(alt="Hero BG" class="" ...)

Compiled html:

<div class="blur-load loaded" style="background-image: url(media/Hero-thumbnail.jpg)">
    <img width="1920" height="1280" alt="Hero BG" src="media/[email protected]" loading="lazy" srcset="media/[email protected] 1366w, media/[email protected] 1600w, media/[email protected] 1920w ">
</div>

Using +imgBlur for Responsive Image mixin with ommited suffix key:

+imgBlur({
    name: 'Hero',
    ext: 'jpg',
    dir: mediaFolder,
    resolutions: [1366,1600, 1920],
    width: 1920,
    height: 1280
})(alt="Hero BG" class="" ...)

Compiled html:

<div class="blur-load loaded" style="background-image: url(media/Hero-blurred.jpg)">
    <img width="1920" height="1280" alt="Hero BG" src="media/[email protected]" loading="lazy" srcset="media/[email protected] 1366w, media/[email protected] 1600w, media/[email protected] 1920w ">
</div>

In that case (ommited suffix) you can just create a tumbnail image with '-blurred' suffix in its pathname via Figma or other app.

Another option (using Gulp Task) for creating thumbnails is described below.

Creating Blurred Thumbnails Easy (using Gulp Task):

gulpfile.js contains a standalone task imgResize for creating thumbnails for the technique described above, where you can configure source file, size (10px usually good enough for blurred image with very low weight) and result file name. Example from gulpfilejs:

function imgResize() {
  return src(`${options.paths.src.media}/[email protected]`)
    .pipe(
      imageResize({
        width: 10
      })
    )
    .pipe(
      rename(function (path) {
        // Updates the object in-place
        path.basename = "hhero-blurred"
      })
    )
    .pipe(dest(`${options.paths.src.media}`))
}

...

...
exports.imgResize = imgResize;
...

Turn Off Blurred Thumbnail Preview Image Loading:

To turn off the feature, remove the code from main.js:

// BLurred Preview image Lazy Loading
if (document.querySelectorAll(".blur-load").length) {
  const blurDivs = document.querySelectorAll(".blur-load");
  blurDivs.forEach(div => {
    const img = document.querySelector("img");
    function loaded() {
      div.classList.add("loaded");
    }

    if (img.complete) {
      loaded();
    } else {
      img.addEventListener("load", loaded);
    }
  });
}

as well as remove style for it from style.css:

/* lazy load img with blur effect start */
  .blur-load {
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    position: relative;
  }
  .blur-load:before {
    content: "";
    position: absolute;
    inset: 0;
    background-color: hsl(0 100% 100% / 0.2);
    opacity: 1;
    animation: pulse 2.5s infinite;
  }
  @keyframes pulse {
    0% {
      opacity: 0;
    }
    50% {
      opacity: 1;
    }
    100% {
      opacity: 0;
    }
  }
  .blur-load.loaded {
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
  }
  .blur-load.loaded::before {
    content: none;
  }
  .blur-load > img {
    object-fit: cover;
    object-position: center;
    opacity: 0;
    transition: opacity 0.5s ease-in-out;
  }
  .blur-load.loaded > img {
    opacity: 1;
  }
  /* lazy load img with blur effect end */

projecttemplate's People

Contributors

vitamineart avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.