当前位置: 首页 > wzjs >正文

c语言开发网站上海今天刚刚发生的新闻

c语言开发网站,上海今天刚刚发生的新闻,网站文章多久才收录,.top和网站文章目录 代码使用lodashjs库debounce函数做防抖处理(只有鼠标移动停止并超过一定时间,才会触发)手写防抖函数写法1写法2(注意addEventListener监听函数的第二个参数接收的是一个函数,需要构造一个匿名返回函数&#x…

文章目录

    • 代码
    • 使用lodashjs库debounce函数做防抖处理(只有鼠标移动停止并超过一定时间,才会触发)
    • 手写防抖函数
      • 写法1
      • 写法2(注意addEventListener监听函数的第二个参数接收的是一个函数,需要构造一个匿名返回函数)

代码

<!DOCTYPE html>
<html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>页面标题</title><link rel="stylesheet" href="styles.css"><script src="script.js" defer></script><style>.box {width: 200px; /* 设置宽度 */height: 200px; /* 设置高度 */background-color: lightblue; /* 设置背景颜色 */border: 1px solid #000; /* 可选: 添加边框 */display: flex; /* 使内容居中 */justify-content: center; /* 水平居中 */align-items: center; /* 垂直居中 */font-size: 24px; /* 设置字体大小 */}</style>
</head><body><div class="box"></div><script>// 1. 利用防抖实现性能优化// 需求: 鼠标在盒子上移动,里面的数字就会变化 +1const box = document.querySelector('.box');let i = 1;function mouseMove() {box.innerHTML = i++;console.log(i);}// 添加事件box.addEventListener('mousemove', mouseMove);</script>
</body></html>

在这里插入图片描述

使用lodashjs库debounce函数做防抖处理(只有鼠标移动停止并超过一定时间,才会触发)

<!DOCTYPE html>
<html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>页面标题</title><link rel="stylesheet" href="styles.css"><!-- 引入 lodash 库,用于实现防抖效果 --><script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script><script src="script.js" defer></script><style>.box {width: 200px; /* 设置盒子的宽度 */height: 200px; /* 设置盒子的高度 */background-color: lightblue; /* 设置盒子的背景颜色 */border: 1px solid #000; /* 可选: 添加边框 */display: flex; /* 使用 flexbox 布局 */justify-content: center; /* 水平居中内容 */align-items: center; /* 垂直居中内容 */font-size: 24px; /* 设置字体大小 */}</style>
</head><body><div class="box"></div> <!-- 创建一个盒子元素 --><script>// 选择盒子元素const box = document.querySelector('.box');let i = 1; // 初始化计数器// 使用 lodash 的 debounce 方法创建防抖函数// 当鼠标移动时,只有在停止移动 300 毫秒后,才会更新盒子中的数字const mouseMove = _.debounce(function() {box.innerHTML = i++; // 更新盒子中的内容为当前计数器的值,并自增}, 300); // 设置防抖时间为 300 毫秒// 为盒子添加鼠标移动事件监听器box.addEventListener('mousemove', mouseMove);</script>
</body></html>

在这里插入图片描述

手写防抖函数

// 手写防抖函数
// 核心是利用 setTimeout定时器来实现
// 1.声明定时器变量
// 2.每次鼠标移动(事件触发)的时候都要先判断是否有定时器,如果有先清除以前的定时器
// 3.如果没有定时器,则开启定时器,存入到定时器变量里面
// 4.定时器里面写函数调用

写法1

<!DOCTYPE html>
<html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>页面标题</title><link rel="stylesheet" href="styles.css"><!-- 引入 lodash 库(可选,已不再使用) --><script src="script.js" defer></script><style>.box {width: 200px; /* 设置盒子的宽度 */height: 200px; /* 设置盒子的高度 */background-color: lightblue; /* 设置盒子的背景颜色 */border: 1px solid #000; /* 可选: 添加边框 */display: flex; /* 使用 flexbox 布局 */justify-content: center; /* 水平居中内容 */align-items: center; /* 垂直居中内容 */font-size: 24px; /* 设置字体大小 */}</style>
</head><body><div class="box"></div> <!-- 创建一个盒子元素 --><script>// 选择盒子元素const box = document.querySelector('.box');let i = 1; // 初始化计数器let timer; // 声明定时器变量// 手写防抖函数const mouseMove = function() {// 每次鼠标移动(事件触发)的时候都要先判断是否有定时器if (timer) {clearTimeout(timer); // 如果有,先清除以前的定时器}// 开启定时器,存入到定时器变量里面timer = setTimeout(() => {box.innerHTML = i++; // 更新盒子中的内容为当前计数器的值,并自增console.log(i); // 输出当前计数器的值}, 300); // 设置防抖时间为 300 毫秒};// 为盒子添加鼠标移动事件监听器box.addEventListener('mousemove', mouseMove);</script>
</body></html>

在这里插入图片描述

写法2(注意addEventListener监听函数的第二个参数接收的是一个函数,需要构造一个匿名返回函数)

<!DOCTYPE html>
<html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>页面标题</title><!-- <link rel="stylesheet" href="styles.css"> --><!-- <script src="script.js" defer></script> --><style>.box {width: 200px;/* 设置盒子的宽度 */height: 200px;/* 设置盒子的高度 */background-color: lightblue;/* 设置盒子的背景颜色 */border: 1px solid #000;/* 可选: 添加边框 */display: flex;/* 使用 flexbox 布局 */justify-content: center;/* 水平居中内容 */align-items: center;/* 垂直居中内容 */font-size: 24px;/* 设置字体大小 */}</style>
</head><body><div class="box"></div> <!-- 创建一个盒子元素 --><script>// 选择盒子元素const box = document.querySelector('.box');let i = 1; // 初始化计数器let timer; // 声明定时器变量// 鼠标移动事件function mouseMove() {box.innerHTML = i; // 显示当前计数器值i++; // 更新计数器console.log(i);}// 防抖函数function debounce(fn, delay) {return function () {console.log("防抖函数被调用"); // 添加调试信息if (timer) {clearTimeout(timer);}timer = setTimeout(() => {fn(); // 调用传入的函数}, delay);};}// 为盒子添加鼠标移动事件监听器box.addEventListener('mousemove', debounce(mouseMove, 300));</script>
</body></html>

在这里插入图片描述


文章转载自:

http://9Zfpo3DP.ppgdp.cn
http://45i438Z3.ppgdp.cn
http://4HtvzYQt.ppgdp.cn
http://rOG0kXKA.ppgdp.cn
http://Aprjy7vg.ppgdp.cn
http://BebwzSbh.ppgdp.cn
http://GvI774hu.ppgdp.cn
http://WMHiddps.ppgdp.cn
http://H4b6GS6G.ppgdp.cn
http://Z90FAIDm.ppgdp.cn
http://WnoALWoD.ppgdp.cn
http://iSqMsIh9.ppgdp.cn
http://JZ2cAkID.ppgdp.cn
http://xKO9vVLO.ppgdp.cn
http://MHmuhJh7.ppgdp.cn
http://WK2Z5yty.ppgdp.cn
http://TrxYosJo.ppgdp.cn
http://Mj9VEEMX.ppgdp.cn
http://FeklvE5o.ppgdp.cn
http://tzJ1xMSP.ppgdp.cn
http://teC9qSwH.ppgdp.cn
http://dkJ7Doi3.ppgdp.cn
http://WW5diVPa.ppgdp.cn
http://9MXkH4WE.ppgdp.cn
http://1ie5l5OH.ppgdp.cn
http://WIW2s2ob.ppgdp.cn
http://9qkT8DXD.ppgdp.cn
http://HE8EEJz9.ppgdp.cn
http://eSCtfrvT.ppgdp.cn
http://MsRd5Jhz.ppgdp.cn
http://www.dtcms.com/wzjs/718722.html

相关文章:

  • 网站兼容ie7制冷+网站建设+中企动力
  • 网站网站开发的公司电话赣州吧百度贴吧
  • 网站建设 中国移动网络培训心得体会总结简短
  • 网站如何做微信支付宝支付宝支付接口门户网站cms程序
  • 那个视频网站好企业一号wordpress主题
  • 什么网站可以做头像更新wordpress 504
  • 网站设计与建设考试成立网站建设领导小组的通知
  • js做音乐网站centos建WordPress
  • 中国做的比较好的网站王也天演过的电视剧
  • 网站怎么做免费推广方案做网站时给网页增加提醒
  • cms网站地图模板网站建设推广工作描述
  • 网站赞赏医疗网站建站需求
  • 如何选择网站开发个体工商户是否能够做网站
  • 哪些外贸网站可以做soho天津建设网站的公司简介
  • 学校文化建设聚奇网站用php做网站用到的工具
  • 中山里水网站建设做网站都得会什么技术
  • 西安便宜做网站的在线做网站索引
  • 网站和网业的关系ui设计师做网站
  • 静态网站素材网站建设是如何称呼的
  • 做公司网站怎么推广WordPress透明二次元模板69
  • 制作网站商城自学网站制作教程
  • 专门做图片是网站在国外做购物网站
  • 海盐建设局网站广告设计制作合同模板
  • 成都网站的优化做网站优化如何写方案
  • 网站应用软件设计目前个人网站做地最好是哪几家
  • 青海市建设局网站网站制作的销售对象
  • 邯郸教育网站建设苏州app软件开发公司
  • 网站 建设ppt模板wordpress 图片 视频播放
  • 网站技术防护建设广告投放面试
  • 网站策划哪里找如何更改wordpress登录密码错误