在Vue2或Vue3中项目中使用 Isotope(同位素) 过滤和排序神奇的布局神器,全网独家实现!

简介: 本文介绍了在Vue2或Vue3项目中如何使用Isotope(同位素)布局库来创建动态的网格布局,并提供了详细的代码实现和效果展示,包括过滤和排序功能。

前言

Isotope是一个用于网格布局的JavaScript库,它可以帮助你创建漂亮的、动态的网格布局。Isotope支持过滤、排序和动画效果,可以让你的网站更加生动有趣。目前,不仅JQuery支持Isotope,还有Vue、React、Angular等这些框架也有对其进行封装,做成了组件,方便引入项目并使用。但是随着Vue等框架版本的升级,这些组件许多没有继续更新,不支持高版本的前端框架。也就是说在高版本前端框架的项目中引入许多年前的组件,或多或少会出现报错导致无法使用。于是,UP在此实现如何在Vue2或Vue3项目中使用 Isotope(同位素)神器,挺简单的,全网独家实现。

Isotope官网:Isotope · Filter & sort magical layouts

一、示例代码

1.Vue2.X写法

(1)/src/views/Example/Isotope/index_vue2.vue

<template>
  <div class="isotope_container">
    <div class="isotope_filter_navbar">
      <span data-filter="*" class="actived">All</span>
      <span data-filter=".Vue">Vue</span>
      <span data-filter=".React">React</span>
      <span data-filter=".Angular">Angular</span>
    </div>

    <div class="isotope_filter_grid">
      <div class="isotope_filter_grid_item Vue">
        <img src="/images/404.png" alt="" />
        <h1>HelloWorld! 1</h1>
      </div>
      <div class="isotope_filter_grid_item Vue">
        <img src="/images/404.png" alt="" />
        <h1>HelloWorld! 2</h1>
      </div>
      <div class="isotope_filter_grid_item Vue React">
        <img src="/images/404.png" alt="" /> 
        <h1>HelloWorld! 3</h1>
      </div>
      <div class="isotope_filter_grid_item Vue React">
        <img src="/images/404.png" alt="" />
        <h1>HelloWorld! 4</h1>
      </div>
      <div class="isotope_filter_grid_item React">
        <img src="/images/404.png" alt="" />
        <h1>HelloWorld! 5</h1>
      </div>
      <div class="isotope_filter_grid_item React">
        <img src="/images/404.png" alt="" />
        <h1>HelloWorld! 6</h1>
      </div>
      <div class="isotope_filter_grid_item Angular">
        <img src="/images/404.png" alt="" />
        <h1>HelloWorld! 7</h1>
      </div>
      <div class="isotope_filter_grid_item Angular">
        <img src="/images/404.png" alt="" />
        <h1>HelloWorld! 8</h1>
      </div>
    </div>
  </div>
</template>

<script>
import Isotope from 'isotope-layout'

export default {
    
    
  data() {
    
    
    return {
    
    
      // ...
    }
  },
  mounted() {
    
    
    this.handleInitIsotope()
  },
  methods: {
    
    
    /**
     * 初始化 Isotope 组件
     */
    async handleInitIsotope() {
    
    
      // 1.获取 isotope 导航条
      const navbar = await document.querySelector('.isotope_filter_navbar')

      // 2.实例化 isotope 对象
      const isotope = await new Isotope(
        '.isotope_filter_grid', // 容器
        {
    
    
          layoutMode: 'fitRows', // 布局模式
          itemSelector: '.isotope_filter_grid_item', // 元素
        }
      )

      // 3.为导航条注册点击事件
      navbar.addEventListener('click', (e) => {
    
    
        const {
    
     target } = e
        const filterOption = target.getAttribute('data-filter') // 筛选的类别
        if (filterOption) {
    
    
          // 给元素移除 actived 样式
          navbar
            .querySelectorAll('span')
            .forEach(
              (btn) => btn.classList.remove('actived')
            )

          // 给目标元素加上 actived 样式
          target.classList.add('actived')

          // 筛选
          isotope.arrange({
    
     filter: filterOption })
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
.isotope_container {
    
    
  position: relative;

  .isotope_filter_navbar {
    
    
    display: flex;
    align-items: center;
    padding: 20px 20px 10px 20px;

    span {
    
    
      display: inline-block;
      color: #999999;
      font-family: gotham-book;
      font-size: 24px;
      font-weight: bold;
      text-transform: uppercase;
      margin: 0px 30px 0 0;
      text-decoration: none;
      transition: all 0.4s ease-in-out;
      text-transform: capitalize; // 首字母大写
      cursor: pointer;

      &.actived {
    
    
        color: #000;
      }

      &:hover {
    
    
        color: #000;
      }
    }
  }

  .isotope_filter_grid {
    
    

    .isotope_filter_grid_item {
    
    
      display: flex;
      width: 25vw;
      height: 20vw;

      img {
    
    
        display: block;
        flex: 1;
        margin: 20px;
        object-fit: cover;
        background-color: #000;
      }

      h1 {
    
    
        display: grid;
        align-items: center;
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        margin: auto;
        color: #fff;
        font-weight: lighter;
      }
    }
  }
}
</style>

2.Vue3.X写法

(1)/src/views/Example/Isotope/index_vue3.vue

<template>
  <div class="isotope_container">
    <div class="isotope_filter_navbar">
      <span data-filter="*" class="actived">All</span>
      <span data-filter=".Vue">Vue</span>
      <span data-filter=".React">React</span>
      <span data-filter=".Angular">Angular</span>
    </div>

    <div class="isotope_filter_grid">
      <div class="isotope_filter_grid_item Vue">
        <img src="/images/clients/404.png" alt="" />
        <h1>HelloWorld! 1</h1>
      </div>
      <div class="isotope_filter_grid_item Vue">
        <img src="/images/clients/404.png" alt="" />
        <h1>HelloWorld! 2</h1>
      </div>
      <div class="isotope_filter_grid_item Vue React">
        <img src="/images/clients/404.png" alt="" /> 
        <h1>HelloWorld! 3</h1>
      </div>
      <div class="isotope_filter_grid_item Vue React">
        <img src="/images/clients/404.png" alt="" />
        <h1>HelloWorld! 4</h1>
      </div>
      <div class="isotope_filter_grid_item React">
        <img src="/images/clients/404.png" alt="" />
        <h1>HelloWorld! 5</h1>
      </div>
      <div class="isotope_filter_grid_item React">
        <img src="/images/clients/404.png" alt="" />
        <h1>HelloWorld! 6</h1>
      </div>
      <div class="isotope_filter_grid_item Angular">
        <img src="/images/clients/404.png" alt="" />
        <h1>HelloWorld! 7</h1>
      </div>
      <div class="isotope_filter_grid_item Angular">
        <img src="/images/clients/404.png" alt="" />
        <h1>HelloWorld! 8</h1>
      </div>
    </div>
  </div>
</template>

<script>
import Isotope from 'isotope-layout'

export default {
    
    
  data() {
    
    
    return {
    
    
      // ...
    }
  },
  mounted() {
    
    
    this.handleInitIsotope()
  },
  methods: {
    
    
    async handleInitIsotope() {
    
    
      // 1.获取 isotope 导航条
      const navbar = await document.querySelector('.isotope_filter_navbar')
      console.log('navbar =>', navbar)

      // 2.实例化 isotope 对象
      const isotope = await new Isotope(
        '.isotope_filter_grid', // 容器
        {
    
    
          layoutMode: 'fitRows', // 布局模式
          itemSelector: '.isotope_filter_grid_item', // 元素
        }
      )
      console.log('isotope =>', isotope)

      // 3.为导航条注册点击事件
      navbar.addEventListener('click', (e) => {
    
    
        const {
    
     target } = e
        console.log('target =>', target)
        const filterOption = target.getAttribute('data-filter') // 筛选的类别
        if (filterOption) {
    
    
          // 给元素移除 actived 样式
          navbar
            .querySelectorAll('span')
            .forEach(
              (btn) => btn.classList.remove('actived')
            )

          // 给目标元素加上 actived 样式
          target.classList.add('actived')

          // 筛选
          isotope.arrange({
    
     filter: filterOption })
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
.isotope_container {
    
    
  position: relative;

  .isotope_filter_navbar {
    
    
    display: flex;
    align-items: center;
    padding: 20px 20px 10px 20px;

    span {
    
    
      display: inline-block;
      color: #999999;
      font-family: gotham-book;
      font-size: 24px;
      font-weight: bold;
      text-transform: uppercase;
      margin: 0px 30px 0 0;
      text-decoration: none;
      transition: all 0.4s ease-in-out;
      text-transform: capitalize; // 首字母大写
      cursor: pointer;

      &.actived {
    
    
        color: #000;
      }

      &:hover {
    
    
        color: #000;
      }
    }
  }

  .isotope_filter_grid {
    
    

    .isotope_filter_grid_item {
    
    
      position: relative;
      display: flex;
      width: 25vw;
      height: 25vw;
      text-align: center;

      img {
    
    
        display: block;
        flex: 1;
        margin: 20px;
        object-fit: cover;
        background-color: #000;
      }

      h1 {
    
    
        display: grid;
        align-items: center;
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        margin: auto;
        color: #fff;
        font-weight: lighter;
      }
    }
  }
}
</style>

二、运行效果

(1)以上效果都是一样的

目录
相关文章
|
3天前
|
JavaScript 容器
乾坤qiankun框架搭建 主应用为vue3的项目。
乾坤qiankun框架搭建 主应用为vue3的项目。
18 2
|
2天前
|
缓存 JavaScript 前端开发
《基础篇第4章:vue2基础》:使用vue脚手架创建项目
《基础篇第4章:vue2基础》:使用vue脚手架创建项目
10 3
|
4天前
|
JavaScript 前端开发
Vue 2 和 Vue 3 之间响应式区别
10月更文挑战第7天
16 2
|
20小时前
|
JavaScript 前端开发 Java
vue2知识点:Vue封装的过度与动画
vue2知识点:Vue封装的过度与动画
7 0
|
5天前
|
存储 JavaScript 前端开发
Vue.js项目中全面解析定义全局变量的常用方法与技巧
Vue.js项目中全面解析定义全局变量的常用方法与技巧
9 0
|
4天前
|
JavaScript 前端开发 开发者
Vue v-for 进阶指南:in 与 of 的区别及应用场景 | 笔记
Vue.js 中的 v-for 是强大的遍历指令,但其中的 in 和 of 关键字往往被开发者忽视。尽管它们的用法相似,但适用的场景和数据结构却各有不同。本文将详细探讨 v-for 中 in 和 of 的区别、适用场景以及在实际开发中的最佳使用时机。通过理解它们的差异,你将能够编写更加高效、简洁的 Vue.js 代码,灵活应对各种数据结构的遍历需求。
40 6
|
2天前
|
缓存 JavaScript
Vue 中 computed 与 method 的区别
【10月更文挑战第15天】computed 和 method 是 Vue 中两个重要的选项,它们在功能和特点上存在着明显的区别。理解并合理运用它们的区别,可以帮助我们构建更高效、更具可维护性的 Vue 应用。在实际开发中,要根据具体情况灵活选择使用,以满足不同的需求。
5 2
|
2天前
|
JavaScript 搜索推荐 UED
vue的自定义指令
【10月更文挑战第14天】Vue 自定义指令为我们提供了一种强大的工具,使我们能够更灵活地控制和扩展 Vue 应用的行为。通过合理地使用自定义指令,可以提高开发效率,增强应用的功能和用户体验。
|
3天前
|
JavaScript
|
4天前
|
缓存 JavaScript 前端开发
Vue 中动态导入的注意事项
【10月更文挑战第12天】 在 Vue 项目中,动态导入是一种常用的按需加载模块的技术,可以提升应用性能和效率。本文详细探讨了动态导入的基本原理及注意事项,包括模块路径的正确性、依赖关系、加载时机、错误处理、缓存问题和兼容性等,并通过具体案例分析和解决方案,帮助开发者更好地应用动态导入技术。