iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >Vue中实现3D标签云的详细代码
  • 552
分享到

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

2024-04-02 19:04:59 552人浏览 八月长安
摘要

预览: 代码: 页面部分: <template> <div class="taGCloud-all" ref="tagcloudall"&

预览:

代码:
页面部分:


<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标签云内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Vue中实现3D标签云的详细代码

本文链接: https://www.lsjlt.com/news/130800.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Vue中实现3D标签云的详细代码
    预览: 代码: 页面部分: <template> <div class="tagcloud-all" ref="tagcloudall"&...
    99+
    2024-04-02
  • Vue中如何实现3D标签云
    今天小编给大家分享一下Vue中如何实现3D标签云的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下...
    99+
    2024-04-02
  • Vue中怎么实现3D标签云
    这篇文章主要讲解了“Vue中怎么实现3D标签云”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Vue中怎么实现3D标签云”吧!预览:代码:页面部分:<template> ...
    99+
    2023-06-20
  • Vue 实现穿梭框功能的详细代码
    Vue - 实现穿梭框功能,效果图如下所示: css .transfer{ display: flex; justify-content: center; ...
    99+
    2024-04-02
  • python中Tkinter实现分页标签的示例代码
    Tkinter实现UI分页标签显示: Input页,红色部分为当前Frame的位置,下半部分为第一页的子标签;三页标签的显示内容各不相同。实现分页显示的核心组件为Radiobutt...
    99+
    2024-04-02
  • vue+elementui+vuex+sessionStorage实现历史标签菜单的示例代码
    一般是有左侧菜单后,然后要在页面上部分添加历史标签菜单需求。 借鉴其他项目,以及网上功能加以组合调整实现 按照标签实现方式步骤来(大致思路): 1,写一个tagNav标签组件 2,...
    99+
    2024-04-02
  • Rust 实现 async/await的详细代码
    目录FutureWake & Context为什么需要 executor ?什么是 waker ?async/awaitExecutorWaker struct 到 ArcW...
    99+
    2024-04-02
  • vue项目中添加electron的详细代码
    1.在package.json中添加 "main": "electron.js", 在 “scripts”: {添加: "package": "electron-pac...
    99+
    2024-04-02
  • Java中outer标签的用法实例代码
    在Java开发的过程中,经常会遇到碰到某些情况从而直接跳出循环,一般来说可以使用break直接跳出循环,但是对于嵌套了好几层的循环,break则显得有些不足,这个时候就可以使用Jav...
    99+
    2023-01-30
    java outer使用 java outer标签用法 java outer
  • Vue中前后端使用WebSocket详细代码实例
    目录什么是websocketwebsocket 原理websocket与http的关系实际开发后端代码总结:什么是websocket WebSocket 是一种网络通信协议。RFC6...
    99+
    2023-03-23
    vue websocket代理 vue websocket消息推送 vue前后端使用websocket
  • Vue+Springboot实现接口签名的示例代码
    1、实现思路 接口签名目的是为了,确保请求参数不会被篡改,请求的数据是否已超时,数据是否重复提交等。 接口签名示意图 客户端提交请求时,将以下参数按照约定签名方式进行签名,随后...
    99+
    2024-04-02
  • vue实现修改标签中的内容:idclassstyle
    目录vue修改标签的内容:id class stylev-bind,v-model注意动态改变class和style的一些方法使用$event下面的函数处理vue修改标签的内容:id...
    99+
    2024-04-02
  • 你们要的Python绘画3D太阳系详细代码
    用Python画一个平面的太阳系得到一些朋友的欣赏,然后有同学提出了绘制三维太阳系的要求。 从Python画图的角度来说,三维太阳系其实并不难,问题在于八大行星对黄道面的倾斜太小,所...
    99+
    2024-04-02
  • MyBatis实现多表联查的详细代码
    目录一、通过映射配置文件实现多表联查二、使用注解的方式一、通过映射配置文件实现多表联查 首先,使用Mysql数据库,创建两个表,分别为学生表Student表和班级表Class表,在S...
    99+
    2022-11-13
    MyBatis多表联查 MyBatis多表查询
  • 从0到1实现GCN——最详细的代码实现
    最近论文中需要使用图卷积神经网络(GNN),看了一些关于GCN的代码,还有基于PyTorch Geometric Temporal的代码实现,在这里做一下记录。 GCN原始代码 关于GCN的原理在这里不进行过多阐述,其他文章里面解释的已经很...
    99+
    2023-10-26
    pytorch 深度学习 人工智能 python
  • C++实现关机功能详细代码
    目录前言:功能实现:总结前言: 可以写出来后发给你的室友或者好朋友,可以增进你们之间的友谊 功能实现: 输入关机命令语句,shutdown -s -t 60,电脑就会在60秒之后关机...
    99+
    2024-04-02
  • Vueechart实现柱状图,电池图,3D柱图和3D圆柱图代码详解
    目录电池图3D柱状图+3个柱图+图例不能点击3D圆筒柱状图3D圆筒柱状图+背景电池图自动播放+底部可拖拽比电池图好看一点的进度条总结电池图 let backgroundColor ...
    99+
    2024-04-02
  • 关于Vue自定义指令实现元素拖动的详细代码
    昨天在做的一个功能时,同时弹出多个框展示多个表格数据。 这些弹出框可以自由拖动。单独的拖动好实现,给元素绑定 mousedowm 事件。 这里就想到了 Vue 里面自定义指令来实现。...
    99+
    2024-04-02
  • C语言实现通讯录的详细代码
    目录(一)实现思路1.通讯录功能2.模块化实现各方面的功能3.代码实现(二)源代码A.test.cB.Contact.hC.Contact.c(一)实现思路 1.通讯录功能 添加好友...
    99+
    2024-04-02
  • Python实现迷宫生成器的详细代码
    作为一项古老的智力游戏,千百年来迷宫都散发着迷人的魅力。但是,手工设计迷宫费时又耗(脑)力,于是,我们有必要制作一个程序:迷宫生成器…… 好吧,我编不下去...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作