这是第二十二个案例,这个例子实现的是鼠标移入a标签时,将其高亮。

附上项目链接: https://github.com/wesbos/JavaScript30

以下为注释后的源码:

<script>
const triggers = document.querySelectorAll('a');
const highlight = document.createElement('span');
highlight.classList.add('highlight');
document.body.appendChild(highlight); function highlightLink() {
// 返回值是一个DOMRect对象,DOMRect 对象包含了一组用于描述边框的只读属性——left、top、right和bottom,单位为像素。
const linkCoords = this.getBoundingClientRect();
const coords = {
width: linkCoords.width,
height: linkCoords.height,
// 这里有个值得注意的点是,因为DOMRect是相对于视口的左上角位置而言的,所以需要加上滚动的scrollY或scrollX
top: linkCoords.top + window.scrollY,
left: linkCoords.left + window.scrollX
}; highlight.style.width = `${coords.width}px`;
highlight.style.height = `${coords.height}px`;
// 除了定位外的另一种移到某个位置的方法,要记得用
highlight.style.transform = `translate(${coords.left}px, ${coords.top}px)`;
} triggers.forEach(a => a.addEventListener('mouseenter', highlightLink));
</script>

最新文章

  1. egret3D与2D混合开发,画布尺寸不一致的问题
  2. oracle10g冷备份和恢复过程记录
  3. UVa 1328 Period
  4. oracle强化练习之分组函数
  5. AS3.0的int uint Number的使用原则
  6. android中listview的一些样式设置
  7. SPJ 讨论
  8. Linux 下搭建jsp服务器(配置jsp开发环境)
  9. LogFactory缺包异常
  10. shell初步了解
  11. 探讨.NET Core数据加密和解密问题
  12. jquery按钮倒计时
  13. fmod函数和modf函数
  14. mybatis-generator 根据表生成对应文件
  15. ListView 无 DataSource 依然用 DataPager 翻页
  16. 微信小程序开发库grace vs wepy
  17. PHP常用方法整理
  18. git心得与总结
  19. 用socket写一个简单的客户端和服务端程序
  20. 【Redis】编译错误zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory

热门文章

  1. bzoj 4603 平凡的骰子
  2. P4455 [CQOI2018]社交网络
  3. codeforces round 417 div2 补题 CF 812 A-E
  4. gerrit使用总结
  5. bzoj1513
  6. 【原创】《从0开始学RocketMQ》—单机搭建
  7. 【SpringCloud构建微服务系列】学习断路器Hystrix
  8. ROS学习笔记四:用C++编写ROS发布与订阅
  9. hihoOffer收割练习20题目2
  10. 找规律 UVALive 6506 Padovan Sequence