目录

      前言

      逛购物网站的时候,想必大家都见过鼠标放到商品上,会有一个放大的效果。今天我们就自己动手封装一个放大镜效果的全局组件,一起来看下吧~

      一、封装的意义

      • 从技术角度
        • 通过vue插件方式封装为全局组件,整个项目其他位置也可以使用,且使用方便
        • 模块化开发思想,一个模块实现一个功能
      • 用户角度
        • 可以带来更好的浏览体验
        • 可以看到商品的细节

      二、如何封装?

      1.  准备

      需要用到@vueuse/core的usemouseinelement方法,所以先在项目根目录下打开终端执行如下命令

      这里安装的指定版本的,各位小伙伴儿按需选择

      npm install @vueuse/core@5.3.0
      

      2.  开始封装

      还是像之前的文章一样,使用vue插件的方式注册全局组件

      在src/components下存放封装的全局组件,这个目录下新建enlarge-images.vue文件。

      代码如下(示例):

      <template>
        <div class="goods-image">
          <!-- 预览大图 -->
          <div class="large" :style='[{backgroundimage: `url(${images[currindex]})`}, bgposition]' v-show='isshow'></div>
          <div class="middle" ref='target'>
            <!-- 左侧的大图 -->
            <img :src="images[currindex]" alt="">
            <!-- 遮罩层 -->
            <div class="layer" :style='[position]' v-show='isshow'></div>
          </div>
          <ul class="small">
            <!-- 右侧的缩略图 -->
             <li v-for="(img,i) in images" :key="img" :class="{active:i===currindex}">
              <img @mouseenter="currindex=i" :src="img" alt="">
            </li>
          </ul>
        </div>
      </template>
      <script>
      import { ref, watch, reactive } from 'vue'
      import { usemouseinelement } from '@vueuse/core'
      
      export default {
        name: 'enlargeimages',
        props: {
          images: {
            type: array,
            default: () => []
          }
        },
        setup (props) {
          const currindex = ref(0)
          const target = ref(null)
          const isshow = ref(false)
          // 遮罩层的坐标
          const position = reactive({
            left: 0,
            top: 0
          })
          // 控制背景图的位置
          const bgposition = reactive({
            backgroundpositionx: 0,
            backgroundpositiony: 0
          })
          const { elementx, elementy, isoutside } = usemouseinelement(target)
          // 侦听鼠标移动后信息
          watch([elementx, elementy, isoutside], () => {
            // 每次有值发生变化,就读取新的数据即可
            isshow.value = !isoutside.value
            // 鼠标在图片的区域之外,不需要计算坐标
            if (isoutside.value) return
            // 水平方向
            if (elementx.value < 100) {
              // 左边界
              position.left = 0
            } else if (elementx.value > 300) {
              // 右边界
              position.left = 200
            } else {
              // 中间的状态
              position.left = elementx.value - 100
            }
            // 垂直方向
            if (elementy.value < 100) {
              // 上边界
              position.top = 0
            } else if (elementy.value > 300) {
              // 下边界
              position.top = 200
            } else {
              // 中间的状态
              position.top = elementy.value - 100
            }
            // console.log(elementx.value, elementy.value, isoutside.value)
            // 计算预览大图背景的位置
            bgposition.backgroundpositionx = -position.left * 2 + 'px'
            bgposition.backgroundpositiony = -position.top * 2 + 'px'
            // 计算左侧遮罩层位置
            position.left += 'px'
            position.top += 'px'
          })
          return { currindex, target, isshow, position, bgposition }
        }
      }
      </script>
      <style scoped lang="less">
      .goods-image {
        box-sizing: border-box;
        width: 480px;
        height: 400px;
        position: relative;
        display: flex;
        z-index: 500;
         img {
              width: 100%;
              height: 100%;
          }
        .large {
          position: absolute;
          top: 0;
          left: 410px;
          width: 400px;
          height: 400px;
          box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
          background-repeat: no-repeat;
          background-size: 800px 800px;
          background-color: #f8f8f8;
        }
        .middle {
          width: 400px;
          height: 400px;
          background: #f5f5f5;
          position: relative;
          cursor: move;
          .layer {
            width: 200px;
            height: 200px;
            background: rgba(0, 0, 0, 0.2);
            left: 0;
            top: 0;
            position: absolute;
          }
        }
        .small {
          margin: 0;
          padding: 0;
          width: 80px;
          li {
            width: 68px;
            height: 68px;
            margin: 10px;
            list-style: none;
            cursor: pointer;
            &:hover,
            &.active {
              border: 2px solid #27ba9b;
            }
          }
        }
      }
      </style>
      
      

      src/components下新建index.js

      import enlargeimages from './enlarge-images.vue'
      
      export default {
        install (app) {
          app.component(enlargeimages.name, enlargeimages)
        }
      }
      

      main.js中注册为插件

      import { createapp } from 'vue'
      import app from './app.vue'
      import router from './router'
      import store from './store'
      // 自己封装的
      import myui from './components'
      
      createapp(app).use(store).use(router).use(myui).mount('#app')
      
      

      3. 使用

      这里借助固定的数据进行测试

      代码如下(示例):

      <template>
        <div class="home-banner">
          <!-- 放大镜效果 -->
          <enlarge-images :images="images"/>
        </div>
      </template>
      
      <script>
      
      export default {
        name: 'app',
        setup() {
          const images = [
            'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2fcloud.jpeg',
            'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2fground.jpeg',
            'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2fnight.jpeg',
            'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2fstreet.jpeg',
            'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2fsun.jpeg'
          ]
      
          return { images }
        }
      }
      </script>
      
      <style lang="less">
      .home-banner {
        width: 1000px;
        margin: 50px auto;
      }
      </style>
      
      

      三、 效果演示

      鼠标移入右侧小图片,即可切换当前显示的图片

      鼠标放入左侧图片预览区,预览区内移动鼠标即可在右侧看到放大的指定区域

      (ps:gif图太大了,各位看下效果图吧~)

      总结

      批量注册为全局组件的方式,各位可以看下这篇文章。

      到此这篇关于如何利用vue3实现放大镜效果的文章就介绍到这了,更多相关vue3实现放大镜效果内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!