vue自定义指令directive实现接口请求转圈的加载效果

11 篇文章 0 订阅
10 篇文章 0 订阅

封装个自定义指令v-Loading实现接口请求前,调用元素在一直转圈的效果,当然ele-UI也有类似的组件,我们是为了实现下,避免有特殊的业务要求,不多说,上代码,先看效果

在这里插入图片描述

vue中配合jq实现的 jq比较少 项目中要是没jq的 可以直接把用到的一行换成原生的就行

/** 注册一个全局指令v-loading */
import Vue from 'vue'

// 插入dom
function insertLoadingDom (el) {
  el.style.position = 'relative'
  // const div = document.createElement('div')
  // div.setAttribute('id', 'loading__child')
  // div.style.cssText = 'position: absolute;left: 0;right: 0;bottom: 0;top: 0;z-index: 999;background: #000;opacity: .5;color: #ffffff;'
  // div.innerHTML = '<span style="position: absolute;top: 50%;left: 50%;transform: translate(-50%,-50%);">loading...</span>'
  // el.appendChild(div)
  const loadDiv = `<div style="width: 100%;height: 100%;position: absolute;background: #ffffff;top:0;left: 0">
                        <div style="color: #000;font-size: 14px;height:100%;width:100%;font-weight: 400;display: flex;justify-content: center;align-items: center">
                            <div class="loadingImg"></div>
                            <div style="margin-left: 6px">正在加载中...</div>
                        </div>
                   </div>`
  $(el).append(loadDiv);
}

// 删除dom
function removeLoadingDom (el) {
  const div = el.querySelector('#loading__child')
  el.removeChild(div)
}

Vue.directive('Loading', {
  // 当被绑定的元素插入到 DOM 中时……
  inserted: function (el, binding) {
    if (binding.value) {
      insertLoadingDom(el)
    }
  },
  update: function (el, binding) {
    if (binding.value) {
      removeLoadingDom(el)
    } else {
      insertLoadingDom(el)
    }
  }
})


还有个转圈动画的实现,去iconfont拿一个半圆的空心圆就行,

<style lang="less">
  .loadingImg{
    width: 16px;
    height: 16px;
    background: url("./assets/img/loading.png") no-repeat;
    background-size: 100% 100%;
    animation: rotate 2s linear infinite;

  }
  @keyframes rotate {
    100% {
      transform: rotate(360deg);
    }
  }
</style>

具体怎么用自定义指令~就不说了吧,各位看官自行补充知识。

该条为vue版本,后面封装个原生基于jq版本的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现小方块的拖拽效果,可以通过自定义指令实现。以下是实现步骤: 1. 在指令中绑定拖拽元素的事件监听器,包括mousedown、mousemove和mouseup事件。 2. 在mousedown事件中记录鼠标按下时的位置和拖拽元素的初始位置。 3. 在mousemove事件中计算鼠标移动的距离,并将拖拽元素的位置进行相应的调整。 4. 在mouseup事件中清除事件监听器。 下面是一个实现小方块拖拽效果自定义指令的示例代码: ```javascript // 注册自定义指令 Vue.directive('drag', { bind: function (el, binding, vnode) { // 记录拖拽元素的初始位置 var initX, initY, startX, startY; // 鼠标按下时的事件处理函数 function mouseDownHandler(e) { initX = el.offsetLeft; initY = el.offsetTop; startX = e.clientX; startY = e.clientY; // 添加事件监听器 document.addEventListener('mousemove', mouseMoveHandler); document.addEventListener('mouseup', mouseUpHandler); } // 鼠标移动时的事件处理函数 function mouseMoveHandler(e) { var deltaX = e.clientX - startX; var deltaY = e.clientY - startY; // 计算拖拽元素的新位置 var newX = initX + deltaX; var newY = initY + deltaY; // 设置拖拽元素的新位置 el.style.left = newX + 'px'; el.style.top = newY + 'px'; } // 鼠标松开时的事件处理函数 function mouseUpHandler() { // 移除事件监听器 document.removeEventListener('mousemove', mouseMoveHandler); document.removeEventListener('mouseup', mouseUpHandler); } // 添加鼠标按下事件监听器 el.addEventListener('mousedown', mouseDownHandler); } }); ``` 使用该指令时,只需要在需要实现拖拽效果的小方块元素上添加v-drag指令即可: ```html <div v-drag class="drag-box"></div> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吴大大逛博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值