• vue中自定义指令的使用场景及示例


     节流指令:

    1. // 1.设置v-throttle自定义指令
    2. Vue.directive('throttle', {
    3. bind: (el, binding) => {
    4. let throttleTime = binding.value; // 防抖时间
    5. if (!throttleTime) { // 用户若不设置防抖时间,则默认2s
    6. throttleTime = 2000;
    7. }
    8. let cbFun;
    9. el.addEventListener('click', event => {
    10. if (!cbFun) { // 第一次执行
    11. cbFun = setTimeout(() => {
    12. cbFun = null;
    13. }, throttleTime);
    14. } else {
    15. event && event.stopImmediatePropagation();
    16. }
    17. }, true);
    18. },
    19. });
    20. // 2.为button标签设置v-throttle自定义指令
    21. <button @click="sayHello" v-throttle>提交button>

    图片懒加载

    1. const LazyLoad = {
    2. // install方法
    3. install(Vue,options){
    4. // 代替图片的loading图
    5. let defaultSrc = options.default;
    6. Vue.directive('lazy',{
    7. bind(el,binding){
    8. LazyLoad.init(el,binding.value,defaultSrc);
    9. },
    10. inserted(el){
    11. // 兼容处理
    12. if('IntersectionObserver' in window){
    13. LazyLoad.observe(el);
    14. }else{
    15. LazyLoad.listenerScroll(el);
    16. }
    17. },
    18. })
    19. },
    20. // 初始化
    21. init(el,val,def){
    22. // data-src 储存真实src
    23. el.setAttribute('data-src',val);
    24. // 设置src为loading图
    25. el.setAttribute('src',def);
    26. },
    27. // 利用IntersectionObserver监听el
    28. observe(el){
    29. let io = new IntersectionObserver(entries => {
    30. let realSrc = el.dataset.src;
    31. if(entries[0].isIntersecting){
    32. if(realSrc){
    33. el.src = realSrc;
    34. el.removeAttribute('data-src');
    35. }
    36. }
    37. });
    38. io.observe(el);
    39. },
    40. // 监听scroll事件
    41. listenerScroll(el){
    42. let handler = LazyLoad.throttle(LazyLoad.load,300);
    43. LazyLoad.load(el);
    44. window.addEventListener('scroll',() => {
    45. handler(el);
    46. });
    47. },
    48. // 加载真实图片
    49. load(el){
    50. let windowHeight = document.documentElement.clientHeight
    51. let elTop = el.getBoundingClientRect().top;
    52. let elBtm = el.getBoundingClientRect().bottom;
    53. let realSrc = el.dataset.src;
    54. if(elTop - windowHeight<0&&elBtm > 0){
    55. if(realSrc){
    56. el.src = realSrc;
    57. el.removeAttribute('data-src');
    58. }
    59. }
    60. },
    61. // 节流
    62. throttle(fn,delay){
    63. let timer;
    64. let prevTime;
    65. return function(...args){
    66. let currTime = Date.now();
    67. let context = this;
    68. if(!prevTime) prevTime = currTime;
    69. clearTimeout(timer);
    70. if(currTime - prevTime > delay){
    71. prevTime = currTime;
    72. fn.apply(context,args);
    73. clearTimeout(timer);
    74. return;
    75. }
    76. timer = setTimeout(function(){
    77. prevTime = Date.now();
    78. timer = null;
    79. fn.apply(context,args);
    80. },delay);
    81. }
    82. }
    83. }
    84. export default LazyLoad;

    关于自定义组件还有很多应用场景,如:拖拽指令、页面水印、权限校验等等应用场景

  • 相关阅读:
    开源协作开发者内容平台Vrite
    电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
    1093 Count PAT‘s
    div的并列和包含关系
    7.前端笔记-CSS-元素显示模式
    语法练习:parrot_trouble
    练[ZJCTF 2019]NiZhuanSiWei
    编程中的信号处理和系统 - 初学者指南
    17 | DataSource 为何物?加载过程是怎样的
    渐进式量产进阶!卡车自动驾驶进入商业化新周期
  • 原文地址:https://blog.csdn.net/weixin_38099055/article/details/133016817