自定义一个回到顶部Vue指令

CSS样式:

<style>
     * {
       margin: 0;
       padding: 0;
     }
     html, body, #app {
       height: 100%;
     }
     #app {
       display: flex;
       flex-direction: column;
       overflow: hidden;
     }
     section {
       flex: 1;
       overflow-y: auto;
     }
     li {
       height: 28px;
       line-height: 28px;
       border-bottom: 1px solid #000000;
     }
     header, footer {
       height: 40px;
       background: red;
     }
     .gotop {
       width: 50px;
       height: 50px;
       position: fixed;
       bottom: 20px;
       right: 20px;
       border: 1px solid blue;;
       border-radius: 50%;
     }
</style>
<body>
    <div id="app">
      <header></header>
      <section v-gotop>
        <ul>
          <li v-for="item in 200" :key="item">{{ item }}</li>
        </ul>
        <!-- <div class="gotop" @click="goTop">goTop</div> -->
      </section>
      <footer></footer>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.11/dist/vue.js"></script>
    <script>
      // goTop 组件的配置对象
      const goTop = {
        data () {
          return {
            visible: false
          }
        },
        template: `
          <div v-show="visible" class="gotop" @click="goTop">点我</div>
        `,
        methods: {
        //点击回到顶部函数
          goTop () {
            this.$el.parentElement.scrollTop = 0
          }
        }
      }
      
        // 注册一个全局自定义指令 `v-gotop`
      Vue.directive('gotop', (el, binding) => {
        // 1. 使用 Vue.extend() 方法来扩展 Vue , 得到一个 Vue 子类
        const GoTop = Vue.extend(goTop)
        // 2. new GoTop 生成 GoTop 的实例
        const instance = new GoTop()
        // 3. instance.$mount() 进行挂载。但是不要提供挂载点。
        //    这个 instance 就有了 $el 属性
        instance.$mount()
        // 4. 就将这个 DOM 对象插入到 el 中
        el.appendChild(instance.$el)
        // 5. 当前元素的滚动条大于可视高度时,出现回到顶部的按钮
        el.addEventListener('scroll', () => {
          if (el.scrollTop >= el.clientHeight) {
            instance.visible = true
          } else {
            instance.visible = false
          }
        })
      })
      const vm = new Vue({
        el: '#app'
      })
    </script>
  </body>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个实现element dialog顶部拖拽的Vue自定义指令,并且限制其不能拖离页面的示例代码: ```javascript // draggable.js export default { inserted(el, binding) { const dialogHeaderEl = el.querySelector('.el-dialog__header') const dialogWrapperEl = el.querySelector('.el-dialog__wrapper') const containmentEl = binding.value ? document.querySelector(binding.value) : dialogWrapperEl let isDragging = false let lastX = 0 let lastY = 0 dialogHeaderEl.style.cursor = 'move' dialogHeaderEl.addEventListener('mousedown', (e) => { isDragging = true lastX = e.clientX lastY = e.clientY document.addEventListener('mousemove', handleMouseMove) document.addEventListener('mouseup', handleMouseUp) }) function handleMouseMove(e) { if (isDragging) { const deltaX = e.clientX - lastX const deltaY = e.clientY - lastY const dialogRect = dialogWrapperEl.getBoundingClientRect() const containmentRect = containmentEl.getBoundingClientRect() let newLeft = dialogRect.left + deltaX let newTop = dialogRect.top + deltaY // Limit the dialog to the containment element if (newLeft < containmentRect.left) { newLeft = containmentRect.left } if (newTop < containmentRect.top) { newTop = containmentRect.top } if (newLeft + dialogRect.width > containmentRect.right) { newLeft = containmentRect.right - dialogRect.width } if (newTop + dialogRect.height > containmentRect.bottom) { newTop = containmentRect.bottom - dialogRect.height } dialogWrapperEl.style.left = newLeft + 'px' dialogWrapperEl.style.top = newTop + 'px' lastX = e.clientX lastY = e.clientY } } function handleMouseUp() { isDragging = false document.removeEventListener('mousemove', handleMouseMove) document.removeEventListener('mouseup', handleMouseUp) } } } ``` 上述代码定义了一个名为`draggable`的Vue自定义指令,可以用于实现element dialog顶部拖拽的效果,并且限制其不能拖离页面。该指令监听了dialog header元素的mousedown、mousemove和mouseup事件,并且根据鼠标的移动距离来实现拖拽效果。同时,通过传入`value`参数来指定限制拖拽区域的父级元素选择器。 使用该自定义指令的示例如下: ```vue <template> <el-dialog v-draggable=".container"> <div class="el-dialog__header"> <span>{{ title }}</span> </div> <div class="el-dialog__body"> {{ content }} </div> </el-dialog> </template> <script> import draggable from '@/directives/draggable' export default { directives: { draggable }, data() { return { title: 'Dialog Title', content: 'Dialog Content' } } } </script> ``` 在上述代码中,我们为el-dialog元素添加了`v-draggable=".container"`指令,表示限制拖拽区域的父级元素选择器为`.container`。注意,我们需要为dialog header元素添加一个类名为`el-dialog__header`的div,以便在指令中获取到该元素进行拖拽。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值