vue3中web前端JS动画案例(四)侧边栏横幅效果-右下角广告-淘宝案例

news2024/9/22 9:35:11

myJsAnimation.js, 这里使用了上次封装的动画方法,并进行了改造

/**
 * 动画的函数
 * dom 当前对象
 * JSON 传入元素对象的属性 {"width": 300, "opacity": 50}
 * 
 * -------------------- 多物体运动,同时运动 ---传入JSON-------------
 */
let speed1 = 0
export function startAnimation2(dom, JSON, fn) {
  // 注意:针对于多物体运动,定时器的返回值要绑定当前的对象中。offsetWidth获取的是包括border的宽度,所以这里使用getComputed获取width
  clearInterval(dom.timer)
  dom.timer = setInterval(() => {
    let cur = 0
    let flag = true // 标杆 如果true,证明所有的属性都到达终点值
    // 0 获取样式属性
    for (let attr in JSON) {
      switch (attr) {
        case 'opacity':
          cur = Math.round(parseFloat(getStyle(dom, attr)) * 100)
          break;
        case 'scrollTop':
          cur = dom[attr]
          break;
        default:
          cur = parseInt(getStyle(dom, attr))
          break;
      }
      // if (attr === 'opacity') {
      //   // 求透明度的变化速度,注意!小数需要取整
      //   cur = Math.round(parseFloat(getStyle(dom, attr)) * 100)
      // } else {
      //   // 获取dom宽度或高度等
      //   cur = parseInt(getStyle(dom, attr))
      // }

      // 1、求速度
      speed1 = (JSON[attr] - cur) / 20
      speed1 = JSON[attr] > cur ? Math.ceil(speed1) : Math.floor(speed1)

      // 2、临界处理
      if (JSON[attr] !== cur) {
        flag = false
      }
      // 3、运动起来
      switch (attr) {
        case 'opacity':
          dom.style[attr] = `alpha(opacity=${cur + speed1})`
          dom.style[attr] = (cur + speed1) / 100
          break;
        case 'scrollTop':
          dom[attr] = cur + speed1
          break;
        default:
          dom.style[attr] = cur + speed1 + 'px'
          break;
      }
      // if (attr === 'opacity') {
      //   dom.style[attr] = `alpha(opacity=${cur + speed1})`
      //   dom.style[attr] = (cur + speed1) / 100
      // } else {
      //   dom.style[attr] = cur + speed1 + 'px'
      // }
    }

    if (flag) {
      clearInterval(dom.timer)
      if (fn) {
        fn()
      }
      return
    }
  }, 30)


  // dom 是对象, attr 是什么属性
  // 获取元素属性的方法
  function getStyle(dom, attr) {
    if (dom.currentStyle) {
      // 针对IE浏览器
      return dom.currentStyle[attr]
    } else {
      // 针对 Firefox浏览器
      return getComputedStyle(dom, null)[attr]
    }
  }
}

 index.vue

<script setup>
import { ref, onMounted, onUnmounted, nextTick, watch, reactive } from 'vue'
import { startAnimation2 } from './MyJSAnimation/myJsAnimation2'
// ----------------------- 08 联动效果 ---------------------
// 1、联动效果
// 2、侧边栏横幅
// 3、滚动监听
// 4、轮播图


// ------------- 1 右下角联动效果 ------------------
const adRef = ref(null)
const close = () => {
  startAnimation2(adRef.value, { "height": 160 }, () => {
    startAnimation2(adRef.value, { "width": 0 }, () => {
      adRef.value.style.display = 'none'
    })
  })
}

// ---------------- 2 左侧边栏横幅 --滚动效果----------------
const asideRef = ref(null)
let aside_top = 0
const raside = ref(null)
const handleScroll = (e) => {
  const lis = raside.value.querySelectorAll('li')

  const scrollTop = window.scrollY || document.documentElement.scrollTop;
  console.log('页面滚动距离:', scrollTop);
  startAnimation2(asideRef.value, { "top": scrollTop + aside_top })

  // 监听页面滚动,选中右边侧边栏
  if (!list.isClick) {
    // 获取页面滚动的高度
    const scrollTop = window.scrollY || document.documentElement.scrollTop;
    for (let i = 0; i < lis.length; i++) {
      if (scrollTop >= list.box[i].offsetTop) {
        list.currentType = list.items[i].type
      }
    }
  }
}

// ----------------3 淘宝案例---------------------
const list = reactive({
  items: [
    { id: 1, name: '爱逛好货', type: '1' },
    { id: 2, name: '好店主播', type: '2' },
    { id: 3, name: '品质特色', type: '3' },
    { id: 4, name: '猜你喜欢', type: '4' }
  ],
  currentType: '1',
  isClick: false, // 是否点击右侧边栏
  box: null, // 所有的大盒子
})
const boxRef = ref(null)
const getStyle = () => {
  // 上色
  const color = ['skyblue', 'orange', 'blue', 'purple']
  for (let index = 0; index < list.box.length; index++) {
    list.box[index].style.backgroundColor = color[index];
  }
}

// 监听右导航器按钮的点击
const handleClickItem = (item, index) => {
  list.isClick = true
  list.currentType = item.type
  nextTick(() => {
    // 文档的顶部到视口顶部的距离 = indx * 文档高度
    // document.documentElement.scrollTop = index * document.body.clientHeight
    // console.log(document.documentElement.scrollTop);
    // 页面动画
    startAnimation2(document.documentElement, { "scrollTop": index * document.body.clientHeight }, () => {
      list.isClick = false
    })

  })
}


onMounted(() => {
  aside_top = asideRef?.value?.offsetTop // 左侧边栏举例顶部的距离
  list.box = boxRef.value.querySelectorAll('div')
  window.addEventListener('scroll', handleScroll);
  getStyle()
})
onUnmounted(() => {
  window.removeEventListener('scroll', handleScroll);
});



</script>

<template>
  <div class="info" id="info">
    <div class="main" id="box" ref="boxRef">
      <div class="current">爱逛好货</div>
      <div>好店主播</div>
      <div>品质特色</div>
      <div>猜你喜欢</div>
    </div>
    <!-- 右导航器 -->
    <ul class="r-aside" ref="raside">
      <li v-for="(item, index) in list.items" :key="item.id"
        :class="`${item.type === list.currentType ? 'active' : ''}`" @click="handleClickItem(item, index)">
        <a href="javascript:void(0)">{{ item.name }}</a>
      </li>

    </ul>
    <!-- 01 右下角广告联动效果 -->
    <div id="ad" ref="adRef">
      <img src="../assets/vue.svg" alt="">
      <span id="close" @click="close">X</span>
    </div>

    <!-- 左侧边栏横幅效果 -->
    <div id="aside" ref="asideRef">
      <img src="../assets/vue.svg" alt="">
    </div>
  </div>
</template>

<style scoped lang="less">
.info {
  display: flex;
  flex-direction: column;
  position: relative;

  .main {
    width: 1190px;
    height: 5000px;
    margin: 0 auto;

    &>div {
      width: 100%;
      height: 100%;
      text-align: center;
      font-size: 30px;
    }
  }


  // 01 联动效果
  #ad {
    position: fixed;
    bottom: 0;
    right: 0;
    background-color: pink;

    img {
      width: 200px;
      height: 200px;
    }

    #close {
      position: absolute;
      top: 0;
      right: 0;
      width: 20px;
      height: 20px;
      text-align: center;
      line-height: 20px;
      cursor: pointer;
      // background-color: skyblue;
      z-index: 5;
    }
  }

  #aside {
    position: absolute;
    top: 200px;
    left: 0;
    // transform: translateY(-50%);
    background-color: pink;

    img {
      width: 100px;
      height: 100px;
    }
  }

  ul {
    list-style: none;
  }

  a {
    text-decoration: none;
  }

  .r-aside {
    position: fixed;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 40px;
    font-size: 16px;
    font-weight: 700;
    text-align: center;

    li {
      height: 50px;
      border-bottom: 1px solid #ddd;

      a {
        color: peru;
      }

      &.active {
        background-color: coral;

        a {
          color: #fff;
        }
      }
    }
  }
}
</style>

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1616884.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Unity Shader 图形学【笔记一】

游戏图形学 源自&#xff1a;计算机图形学 涵盖&#xff1a;图形、动画的创建渲染展示 目标&#xff1a;性能优化、提高视觉质量&#xff0c;增强用户体验 技术&#xff1a;三维模型、纹理、光照、阴影、特效、动画、物理模拟、碰撞检测等 Unity Shader 是&#xff1a;un…

力扣-1844.将所有数字用字符替换

思路&#xff1a; 1. class Solution:&#xff1a;定义了一个名为 Solution 的类&#xff0c;用于包装解决问题的方法。 2. def replaceDigits(self, s: str) -> str:&#xff1a;定义了一个名为 replaceDigits 的方法&#xff0c;该方法接受一个字符串 s 作为参数&#x…

gitee / github 配置git, 实现免密码登录

文章目录 怎么配置公钥和私钥验证配置成功问题 怎么配置公钥和私钥 以下内容参考自 github ssh 配置&#xff0c;gitee的配置也是一样的&#xff1b; 粘贴以下文本&#xff0c;将示例中使用的电子邮件替换为 GitHub 电子邮件地址。 ssh-keygen -t ed25519 -C "your_emai…

Kotlin语法快速入门--条件控制和循环语句(2)

Kotlin语法入门–条件控制和循环语句&#xff08;2&#xff09; 文章目录 Kotlin语法入门--条件控制和循环语句&#xff08;2&#xff09;二、条件控制和循环语句1、if...else2、when2.1、常规用法2.2、特殊用法--并列&#xff1a;2.3、特殊用法--类型判断&#xff1a;2.4、特殊…

OLED透明屏的寿命如何?

OLED透明屏的寿命一般在20年左右&#xff0c;但如果是旧款&#xff0c;其寿命可能会缩短至15年左右。然而&#xff0c;这个数字并不是绝对的&#xff0c;因为不同用户的使用和保养方法会导致OLED透明屏的寿命出现延长或缩短。例如&#xff0c;长时间使用或在强光、高对比度高亮…

力扣刷题学习python(跟随视频学着刷)

使用入门 视频链接 【手把手带你刷Leetcode力扣&#xff5c;各个击破数据结构和算法&#xff5c;大厂面试必备技能【已完结】-哔哩哔哩】 https://b23.tv/vIcRT61 时空复杂度 时间&#xff1a; 空间&#xff1a;主要有O(1)和O(n)两种 数组 特点&#xff1a;适合读多写少 操作…

Leetcode 118 杨辉三角

目录 一、问题描述二、示例及约束三、代码方法一&#xff1a;数学 四、总结 一、问题描述 给定一个非负整数 numRows&#xff0c;生成「杨辉三角」的前 numRows 行。   在「杨辉三角」中&#xff0c;每个数是它左上方和右上方的数的和。 二、示例及约束 示例 1&#xff1a…

SQL的基础语句

1、select语句 select colums from table_name 2、条件语句 #查询出查询出用户id为1和3的用户记录 IN 操作符允许我们在 WHERE 子句中规定多个值。 select * from student where id in (1,3) #查询出所有姓王的同学 模糊查询 like 通配符(% 任意多个字符 _单个字符) #下例…

AR HUD_VSLAM+显示技术

智能座舱的一个重要技术方向是表达与展示。HUD可以将驾驶相关的信息&#xff0c;如车速、导航等投射到驾驶员的视线上方&#xff0c;避免驾驶员的目光离开前方道路。这种显示方式可以提供关键信息的实时展示&#xff0c;减少驾驶员的分心。 HUD的技术原理就是通过光学系统将信息…

突破文化壁垒:海外社交媒体营销的内容创新与个性化策略

随着全球化的加速和互联网的普及&#xff0c;海外社交媒体营销已成为企业拓展国际市场、提升品牌影响力的重要手段。然而&#xff0c;如何在众多品牌中脱颖而出&#xff0c;吸引海外用户的关注&#xff0c;却是一个值得深入探讨的问题。本文Nox聚星将和大家从内容创新和个性化策…

考研日常记录(upd 24.4.22)

由于实在太无聊了 &#xff0c; 所以记录以下考研备考日常 &#xff0c; 增加一点成就感 &#xff0c; 获得一点前进动力。 文章目录 2024.4.18 周四课程情况&#xff1a;时间规划&#xff1a; 2024.4.19 周五课程情况&#xff1a;时间规划&#xff1a; 2024.4.20 周六2024.4.2…

GaussianEditor:快速可控的3D编辑与高斯飞溅

GaussianEditor: Swift and Controllable 3D Editing with Gaussian Splatting GaussianEditor&#xff1a;快速可控的3D编辑与高斯飞溅 Yiwen Chen*​1,2   Zilong Chen*​3,5   Chi Zhang2   Feng Wang3   Xiaofeng Yang2 陈怡雯 *​1,2 陈子龙 *​3,5 张驰 2 王峰 3 杨晓…

k8s集群资源编排清单文件解读

1、YAML 文件概述 k8s集群中对资源管理和资源对象编排部署都可以通过声明样式&#xff08;YAML&#xff09;文件来解决&#xff0c;也就是可以把需要对资源对象操作编辑到 YAML 格式文件中&#xff0c;我们把这种文件叫做资源清单文件&#xff0c;通过 kubectl 命令直接使用资源…

LTD271次升级 | 网站/小程序可设访问IP的黑白名单 • 官微中心支持PDF等办公文件预览与并分享 • 订单退款显示更详尽明细

1、新增IP访问限制功能&#xff1b; 2、订单新增交易号显示与退款明细显示&#xff1b; 3、自定义地址增加四级地区&#xff1b; 4、Android版App优化文件功能&#xff1b; 5、已知问题修复与优化&#xff1b; 01 官微中心 1) 新增IP限制访问功能 允许或者禁止某些 IP 或…

vue快速入门(四十)非父子组件通信

注释很详细&#xff0c;直接上代码 上一篇 新增内容 媒介js的创建发送组件发送事件示例接收组件接收事件示例 源码 App.vue <template><div id"app"><TessFirst></TessFirst><TestSecond></TestSecond></div> </templ…

Linux 引导过程与服务控制

Linux操作系统引导过程 完整启动过程 简化紧要关键步骤&#xff08;重点&#xff09; Linux系统开机引导过程&#xff1a; 1&#xff09;开机自检 检测硬件设备&#xff0c;找到能够引导系统的设备&#xff0c;比如硬盘 2&#xff09;MBR引导 运行MBR扇区里的主…

【STM32+HAL+Proteus】系列学习教程---RS485总线(收发仿真实现)

实现目标 1、掌握UART/USART/RS485等几个常见概念的区别 2、掌握RS485的逻辑电平、硬件接线等基础知识 3、具体实现目标&#xff1a;1、利用两个单片机组成RS485通信网络&#xff1b;2、两个单片机之间能实现正常收发数据。 一、串口、RS485等之间的关系 串口&#xff1a;是…

使用 Docker 部署 Draw.io 在线流程图系统

1&#xff09;介绍 Draw.io GitHub&#xff1a;https://github.com/jgraph/drawio Draw.io 是一款开源的绘制流程图的工具&#xff0c;拥有大量免费素材和模板。程序本身支持中文在内的多国语言&#xff0c;创建的文档可以导出到多种网盘或本地。无论是创建流程图、组织结构图…

vscode 解决无法创建临时文件。

报错&#xff1a; Fatal error: cant create C:\Users???y\AppData\Local\Temp\ccqkCS9j.o: No such file or directory 右击此 电脑 -> 属性 打开 系统信息 -> 高级系统设置 系统属性 -> 高级 -> 环境变量 将temp 和 tmp 改为其它英文路径 只更改用户变量…

Opencv_2_ 图像色彩空间转换

ColorInvert.h 内容如下&#xff1a; #pragma once #include <opencv.hpp> using namespace std; #include <opencv.hpp> using namespace cv; using namespace std; class ColorInvert{ public : void colorSpaceInvert(Mat&image); }; ColorInvert.cpp…