[项目篇]vue3+ts 移动端和pc端双端实现瀑布流 - 第六天

简介: [项目篇]vue3+ts 移动端和pc端双端实现瀑布流 - 第六天

实现效果



(最近加班有点严重,睡眠不足,所以没有认真的排版,效果是出来了。。将就一下吧)

这是移动端和pc端都可以实现的,基于javascript,进行高度捕获差值去实现的。


网络异常,图片无法展示
|


html部分


<template>
  <div class="row f12" id="app-mains">
      <div class="col-6">
        <div class="card">
          <div class="card-body">
            <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
          </div>
        </div>
      </div>
      <div class="col-6">
        <div class="card">
          <div class="card-body">
            <p class="card-text">This is a short card.</p>
          </div>
        </div>
      </div>
      <div class="col-6">
        <div class="card">
          <div class="card-body">
            <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
          </div>
        </div>
      </div>
      <div class="col-6">
        <div class="card">
          <div class="card-body">
            <p class="card-text">This is a short card.</p>
          </div>
        </div>
      </div>
      <div class="col-6">
        <div class="card">
          <div class="card-body">
            <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
          </div>
        </div>
      </div>
      <div class="col-6">
        <div class="card">
          <div class="card-body">
            <p class="card-text">This is a short card.</p>
          </div>
        </div>
      </div>
  </div>
</template>
![image.png](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5de0a5613fb940768aa9314759dc7710~tplv-k3u1fbpfcp-watermark.image)
复制代码


css部分

由于这本来是在公司实现的,公司的代码就不展示了,处理了一下,变成移动端了,可是是基于bootstrap5.0去实现的,有点懒


<style scoped>
#app-mains{position: relative}
</style>
<style>
  .col-6{padding-left: 0!important;padding-right: 0!important;margin-top: 0!important;}
</style>
复制代码


js部分


<script lang="ts">
import { defineComponent, reactive, toRefs, onMounted } from 'vue'
import 'bootstrap/dist/css/bootstrap.min.css'
interface waterfallFlow {
  waterfallFlowHeight: Array
}
export default {
  name: 'demo',
  setup() {
      const state: waterfallFlow = reactive({
          waterfallFlowHeight: [0, 0]
      })
      const waterfallFlowFun = () => {
          const dom = document.querySelectorAll('.col-6')
          dom.forEach((item: any) => {
              item.style.position = 'absolute'
              const minIndex = filterMin()
              item.style.top = state.waterfallFlowHeight[minIndex] + 10 + 'px'
              item.style.left = minIndex * (100 / 2) + '%'
              state.waterfallFlowHeight[minIndex] += item.querySelector('.card').offsetHeight + 10
          })
      }
      const filterMin = () => {
          const min = Math.min.apply(null, state.waterfallFlowHeight)
          return state.waterfallFlowHeight.indexOf(min)
      }
      const _isMobile = () => {
          let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
          return flag
      }
      onMounted(() => waterfallFlowFun())
  }
}
</script>
复制代码


讲解:


waterfallFlowHeight 这个变量,如果你想两行的瀑布流,这里数据就两个数据,如果三行就三个,以此类推

filter这个函数,是用了Math.min.apply去取最小值(Math.max.apply是取最大值) 这个函数取到了上一行的高度最小值

_isMobile() 这个函数,是用来判断pc端还是web端。由于我是做响应式,在移动端是不需要这个特效的,所以我只判断在pc端实现

相关文章
|
3天前
|
移动开发 前端开发
ruoyi-nbcio-plus基于vue3的flowable为了适配文件上传改造VForm3的代码记录
ruoyi-nbcio-plus基于vue3的flowable为了适配文件上传改造VForm3的代码记录
13 1
|
3天前
|
JavaScript 前端开发
vue2升级到vue3的一些使用注意事项记录(四)
vue2升级到vue3的一些使用注意事项记录(四)
|
3天前
|
移动开发 前端开发
ruoyi-nbcio-plus基于vue3的flowable收回任务后重新进行提交表单的处理
ruoyi-nbcio-plus基于vue3的flowable收回任务后重新进行提交表单的处理
|
3天前
|
移动开发 前端开发
ruoyi-nbcio-plus基于vue3的flowable多租户机制
ruoyi-nbcio-plus基于vue3的flowable多租户机制
|
3天前
|
移动开发 前端开发
ruoyi-nbcio-plus基于vue3的flowable的消息中心我的消息的升级修改
ruoyi-nbcio-plus基于vue3的flowable的消息中心我的消息的升级修改
|
4天前
|
JavaScript
VUE里的find与filter使用与区别
VUE里的find与filter使用与区别
13 0
|
3天前
|
JavaScript
vue页面加载时同时请求两个接口
vue页面加载时同时请求两个接口
|
3天前
|
JavaScript
vue里样式不起作用的方法,可以通过deep穿透的方式
vue里样式不起作用的方法,可以通过deep穿透的方式
12 0
|
3天前
|
移动开发 JavaScript 应用服务中间件
vue打包部署问题
Vue项目`vue.config.js`中,`publicPath`设定为&quot;/h5/party/pc/&quot;,在线环境基于打包后的`dist`目录,而非Linux的`/root`。Nginx代理配置位于`/usr/local/nginx/nginx-1.13.7/conf`,包含两个相关配置图。
vue打包部署问题
|
4天前
|
JavaScript 前端开发
iconfont 图标在vue里的使用
iconfont 图标在vue里的使用
15 0