Vue中实现3D标签云的详细代码

 更新时间:2021年7月20日 10:00  点击:2212

预览:

代码:
页面部分:

<template>
  <div class="tagcloud-all"
       ref="tagcloudall">
    <a v-for="item in tagList" :href="item.url" rel="external nofollow"  :style="'color:' + item.color + ';top: 0;left: 0;filter:none;'">{{item.name}}</a>
  </div>
</template>

CSS部分:

// 标签云
.tagcloud-all {
  position: relative;
  a {
    position: absolute;
    top: 0px;
    left: 0px;
    color: #fff;
    font-weight: bold;
    text-decoration: none;
    padding: 3px 6px;
    &:hover {
      color: #FF0000;
      letter-spacing: 2px;
    }
  }
}

JS部分:

export default {
  name: "tagcloud",
  data() {
    return {
      tagList: [],
      radius: 120,
      dtr: Math.PI / 180,
      d: 300,
      mcList: [],
      active: false,
      lasta: 1,
      lastb: 1,
      distr: true,
      tspeed: 10,
      size: 250,
      mouseX: 0,
      mouseY: 0,
      howElliptical: 1,
      oList: null,
      oA: null,
      sa: 0,
      ca: 0,
      sb: 0,
      cb: 0,
      sc: 0,
      cc: 0
    }
  },
  methods: {
    // 生成随机数
    getRandomNum() {
      return Math.floor(Math.random() * (255 + 1));
    },
    // 三角函数角度计算
    sineCosine(a, b, c) {
      this.sa = Math.sin(a * this.dtr);
      this.ca = Math.cos(a * this.dtr);
      this.sb = Math.sin(b * this.dtr);
      this.cb = Math.cos(b * this.dtr);
      this.sc = Math.sin(c * this.dtr);
      this.cc = Math.cos(c * this.dtr);
    },
    // 设置初始定位
    positionAll() {
      this.$nextTick(() => {      // 注意: 所有的在onReady方法中执行的方法都需要$nextTick确保所有的标签都已经渲染
        var phi = 0;
        var theta = 0;
        var max = this.mcList.length;
        var aTmp = [];
        var oFragment = document.createDocumentFragment();
        // 随机排序
        for (let i = 0; i < this.tagList.length; i++) {
          aTmp.push(this.oA[i]);
        }
        aTmp.sort(() => {
          return Math.random() < 0.5 ? 1 : -1;
        });
        for (let i = 0; i < aTmp.length; i++) {
          oFragment.appendChild(aTmp[i]);
        }
        this.oList.appendChild(oFragment);
        for (let i = 1; i < max + 1; i++) {
          if (this.distr) {
            phi = Math.acos(-1 + (2 * i - 1) / max);
            theta = Math.sqrt(max * Math.PI) * phi;
          } else {
            phi = Math.random() * (Math.PI);
            theta = Math.random() * (2 * Math.PI);
          }
          // 坐标变换
          this.mcList[i - 1].cx = this.radius * Math.cos(theta) * Math.sin(phi);
          this.mcList[i - 1].cy = this.radius * Math.sin(theta) * Math.sin(phi);
          this.mcList[i - 1].cz = this.radius * Math.cos(phi);
          this.oA[i - 1].style.left = this.mcList[i - 1].cx + this.oList.offsetWidth / 2 - this.mcList[i - 1].offsetWidth / 2 + 'px';
          this.oA[i - 1].style.top = this.mcList[i - 1].cy + this.oList.offsetHeight / 2 - this.mcList[i - 1].offsetHeight / 2 + 'px';
        }
      })
    },
    // 坐标更新 让标签动起来
    update() {
      this.$nextTick(() => {           // 注意: 所有的在onReady方法中执行的方法都需要$nextTick确保所有的标签都已经渲染
        var a;
        var b;
        if (this.active) {
          a = (-Math.min(Math.max(-this.mouseY, -this.size), this.size) / this.radius) * this.tspeed;
          b = (Math.min(Math.max(-this.mouseX, -this.size), this.size) / this.radius) * this.tspeed;
        } else {
          a = this.lasta * 0.98;
          b = this.lastb * 0.98;
        }
        this.lasta = a;
        this.lastb = b;
        if (Math.abs(a) <= 0.01 && Math.abs(b) <= 0.01) {
          return
        }
        var c = 0;
        this.sineCosine(a, b, c);
        for (var j = 0; j < this.mcList.length; j++) {
          var rx1 = this.mcList[j].cx;
          var ry1 = this.mcList[j].cy * this.ca + this.mcList[j].cz * (-this.sa);
          var rz1 = this.mcList[j].cy * this.sa + this.mcList[j].cz * this.ca;
          var rx2 = rx1 * this.cb + rz1 * this.sb;
          var ry2 = ry1;
          var rz2 = rx1 * (-this.sb) + rz1 * this.cb;
          var rx3 = rx2 * this.cc + ry2 * (-this.sc);
          var ry3 = rx2 * this.sc + ry2 * this.cc;
          var rz3 = rz2;
          this.mcList[j].cx = rx3;
          this.mcList[j].cy = ry3;
          this.mcList[j].cz = rz3;
          var per = this.d / (this.d + rz3);
          this.mcList[j].x = (this.howElliptical * rx3 * per) - (this.howElliptical * 2);
          this.mcList[j].y = ry3 * per;
          this.mcList[j].scale = per;
          this.mcList[j].alpha = per;
          this.mcList[j].alpha = (this.mcList[j].alpha - 0.6) * (10 / 6);
        }
        this.doPosition();
        this.depthSort();
      })
    },
    //
    doPosition() {
      this.$nextTick(() => {            // 注意: 所有的在onReady方法中执行的方法都需要$nextTick确保所有的标签都已经渲染
        var l = this.oList.offsetWidth / 2;
        var t = this.oList.offsetHeight / 2;
        for (var i = 0; i < this.mcList.length; i++) {
          this.oA[i].style.left = this.mcList[i].cx + l - this.mcList[i].offsetWidth / 2 + 'px';
          this.oA[i].style.top = this.mcList[i].cy + t - this.mcList[i].offsetHeight / 2 + 'px';
          this.oA[i].style.fontSize = Math.ceil(12 * this.mcList[i].scale / 2) + 8 + 'px';
          // this.oA[i].style.filter = "alpha(opacity=" + 100 * this.mcList[i].alpha + ")";
          this.oA[i].style.opacity = this.mcList[i].alpha;
        }
      })
    },
    //
    depthSort() {
      this.$nextTick(() => {            // 注意: 所有的在onReady方法中执行的方法都需要$nextTick确保所有的标签都已经渲染
        var aTmp = [];
        for (let i = 0; i < this.oA.length; i++) {
          aTmp.push(this.oA[i]);
        }
        aTmp.sort(function (vItem1, vItem2) {
          if (vItem1.cz > vItem2.cz) {
            return -1;
          } else if (vItem1.cz < vItem2.cz) {
            return 1;
          } else {
            return 0;
          }
        });
        for (let i = 0; i < aTmp.length; i++) {
          aTmp[i].style.zIndex = i;
        }
      })
    },
    // 网络请求 拿到tagList
    query() {
      // 假装从接口拿回来的数据
      let tagListOrg = [
        { name: '标签1', url: 'www.baidu.com' },
        { name: '标签2', url: 'www.baidu.com' },
        { name: '标签3', url: 'www.baidu.com' },
        { name: '标签4', url: 'www.baidu.com' },
        { name: '标签5', url: 'www.baidu.com' },
        { name: '标签6', url: 'www.baidu.com' },
        { name: '标签7', url: 'www.baidu.com' },
        { name: '标签8', url: 'www.baidu.com' },
        { name: '标签9', url: 'www.baidu.com' },
        { name: '标签10', url: 'www.baidu.com' },
        { name: '标签11', url: 'www.baidu.com' },
        { name: '标签12', url: 'www.baidu.com' },
        { name: '标签13', url: 'www.baidu.com' },
        { name: '标签14', url: 'www.baidu.com' },
        { name: '标签15', url: 'www.baidu.com' },
        { name: '标签16', url: 'www.baidu.com' },
        { name: '标签16', url: 'www.baidu.com' },
        { name: '标签16', url: 'www.baidu.com' },
        { name: '标签16', url: 'www.baidu.com' },
        { name: '标签16', url: 'www.baidu.com' },
        { name: '标签16', url: 'www.baidu.com' },
        { name: '标签16', url: 'www.baidu.com' },
        { name: '标签16', url: 'www.baidu.com' },
        { name: '标签16', url: 'www.baidu.com' },
        { name: '标签16', url: 'www.baidu.com' },
        { name: '标签16', url: 'www.baidu.com' },
        { name: '标签16', url: 'www.baidu.com' },
        { name: '标签16', url: 'www.baidu.com' },
        { name: '标签16', url: 'www.baidu.com' },
        { name: '标签17', url: 'www.baidu.com' }
      ];
      // 给tagList添加随机颜色
      tagListOrg.forEach(item => {
        item.color = "rgb(" + this.getRandomNum() + "," + this.getRandomNum() + "," + this.getRandomNum() + ")";
      })
      this.tagList = tagListOrg;
      this.onReady();
    },
    // 生成标签云
    onReady() {
      this.$nextTick(() => {
        this.oList = this.$refs.tagcloudall;
        this.oA = this.oList.getElementsByTagName('a')
        var oTag = null;
        for (var i = 0; i < this.oA.length; i++) {
          oTag = {};
          oTag.offsetWidth = this.oA[i].offsetWidth;
          oTag.offsetHeight = this.oA[i].offsetHeight;
          this.mcList.push(oTag);
        }
        this.sineCosine(0, 0, 0);
        this.positionAll();
        this.oList.onmouseover = () => {
          this.active = true;
        }
        this.oList.onmouseout = () => {
          this.active = false;
        }
        this.oList.onmousemove = (event) => {
          var oEvent = window.event || event;

          this.mouseX = oEvent.clientX - (this.oList.offsetLeft + this.oList.offsetWidth / 2);
          this.mouseY = oEvent.clientY - (this.oList.offsetTop + this.oList.offsetHeight / 2);
          this.mouseX /= 5;
          this.mouseY /= 5;
        }
        setInterval(() => {
          this.update()
        }, 30);            // 定时器执行 不能写setInterval(this.update(), 30)
      })
    }
  },
  created() {
    this.$nextTick(() => {
      this.query();
    })
  }
}

到此这篇关于Vue中实现3D标签云的文章就介绍到这了,更多相关Vue 3D标签云内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

[!--infotagslink--]

相关文章

  • vue中activated的用法

    这篇文章主要介绍了vue中activated的用法,帮助大家更好的理解和使用vue框架,感兴趣的朋友可以了解下...2021-01-03
  • 基于vue-simple-uploader封装文件分片上传、秒传及断点续传的全局上传插件功能

    这篇文章主要介绍了基于vue-simple-uploader封装文件分片上传、秒传及断点续传的全局上传插件,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-23
  • Antd-vue Table组件添加Click事件,实现点击某行数据教程

    这篇文章主要介绍了Antd-vue Table组件添加Click事件,实现点击某行数据教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-17
  • Vue组件跨层级获取组件操作

    这篇文章主要介绍了Vue组件跨层级获取组件操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-28
  • vue 实现动态路由的方法

    这篇文章主要介绍了vue 实现动态路由的方法,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-06
  • antdesign-vue结合sortablejs实现两个table相互拖拽排序功能

    这篇文章主要介绍了antdesign-vue结合sortablejs实现两个table相互拖拽排序功能,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-01-09
  • vue 获取到数据但却渲染不到页面上的解决方法

    这篇文章主要介绍了vue 获取到数据但却渲染不到页面上的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-19
  • Vue基于localStorage存储信息代码实例

    这篇文章主要介绍了Vue基于localStorage存储信息代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-11-16
  • vue项目页面嵌入代码块vue-prism-editor的实现

    这篇文章主要介绍了vue项目页面嵌入代码块vue-prism-editor的实现,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-10-30
  • Vue 3.0中jsx语法的使用

    这篇文章主要介绍了Vue 3.0 中 jsx 语法使用,帮助大家更好的理解和使用vue框架,感兴趣的朋友可以了解下...2020-11-13
  • vue Treeselect下拉树只能选择第N级元素实现代码

    这篇文章主要介绍了vue Treeselect下拉树只能选择第N级元素实现代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-01
  • vue项目多环境配置(.env)的实现

    最常见的多环境配置,就是开发环境配置,和生产环境配置,本文主要介绍了vue项目多环境配置的实现,感兴趣的可以了解一下...2021-07-20
  • vue实现同时设置多个倒计时

    这篇文章主要为大家详细介绍了vue实现同时设置多个倒计时,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-05-20
  • Vue select 绑定动态变量的实例讲解

    这篇文章主要介绍了Vue select 绑定动态变量的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-10-23
  • Ant design vue table 单击行选中 勾选checkbox教程

    这篇文章主要介绍了Ant design vue table 单击行选中 勾选checkbox教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-10-25
  • vue实现div单选多选功能

    这篇文章主要为大家详细介绍了vue实现div单选多选功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-07-16
  • vue接口请求加密实例

    这篇文章主要介绍了vue接口请求加密实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-12
  • Vue生命周期activated之返回上一页不重新请求数据操作

    这篇文章主要介绍了Vue生命周期activated之返回上一页不重新请求数据操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-26
  • 解决vue的router组件component在import时不能使用变量问题

    这篇文章主要介绍了解决vue的router组件component在import时不能使用变量问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-27
  • vue中的插槽详解

    这篇文章主要介绍了Vue中的插槽,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-10-19