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

API-元素尺寸与位置

学习目标:

  • 掌握元素尺寸与位置

学习内容:

  1. 元素尺寸与位置
  2. 仿京东固定导航栏案例
  3. 实现bilibili点击小滑块移动效果

元素尺寸与位置:

  • 使用场景:

前面案例滚动多少距离,都是我们自己算的,最好是页面滚动到某个元素,就可以做某些事。

简单说,就是通过js的方式,得到元素在页面中的位置

这样我们可以做,页面滚动到这个位置,就可以做某些操作,省去计算了。
在这里插入图片描述

  • 获取宽高:

获取元素的自身宽高、包含元素自身设置的宽高、padding、border

offsetWidth和offsetHeight

获取出来的是数值,方便计算。

注意:获取的是可视宽高,如果盒子是隐藏的,获取的结果是0。

  • 获取位置:
  1. offsetLeft和offsetTop 注意是只读属性

获取元素距离自己定位父级元素的左、上距离。

在这里插入图片描述

 <title>offsetLeft</title><style>div {position: relative;width: 200px;height: 200px;background: pink;margin: 100px;}p {width: 100px;height: 100px;background: purple;margin: 50px;}</style>
</head><body><div><p></p></div><script>const div = document.querySelector('div')const p = document.querySelector('p')// console.log(div.offsetLeft)//检测盒子的位置  最近一级带有定位的祖先元素console.log(p.offsetLeft)</script></body>
  1. element.getBoundingClientRect()

方法返回元素大小及其相对于视口的位置。

在这里插入图片描述

 <title>获取元素大小位置的另外方法</title><style>body {height: 2000px;}div {width: 200px;height: 200px;background: pink;margin: 100px;}</style>
</head><body><div></div><script>const div = document.querySelector('div')console.log(div.getBoundingClientRect())</script></body>
  • 小结
  1. offsetWidthoffsetHeight是得到元素什么的宽高?

     内容 + padding + border
    
  2. offsetTopoffsetLeft得到位置以谁为准?

    带有定位的父级
    如果都没有则以文档左上角为准
    
属性作用说明
scrollLeftscrollTop 被卷去的头部和左侧配合页面滚动来用,可读写
clientWidthclientHeight获得元素宽度和高度不包含border、margin,滚动条 ,用于js获取元素大小,只读属性
offsetWidthoffsetHeight获得元素宽度和高度包含border、margin,滚动条,只读
offsetTopoffsetLeft获取元素距离自己定位父级元素的左、上距离获取元素位置的时候使用,只读属性

仿京东固定导航栏案例:

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>练习-仿京东固定导航栏案例</title><style>* {margin: 0;padding: 0;box-sizing: border-box;}.content {overflow: hidden;width: 1000px;height: 3000px;background-color: pink;margin: 0 auto;}.backtop {display: none;width: 50px;left: 50%;margin: 0 0 0 505px;position: fixed;bottom: 60px;z-index: 100;}.backtop a {height: 50px;width: 50px;background: url(./images/bg2.png) 0 -600px no-repeat;opacity: 0.35;overflow: hidden;display: block;text-indent: -999em;cursor: pointer;}.header {position: fixed;top: -80px;left: 0;width: 100%;height: 80px;background-color: purple;text-align: center;color: #fff;line-height: 80px;font-size: 30px;transition: all .3s;}.sk {width: 300px;height: 300px;background-color: skyblue;margin-top: 500px;}</style>
</head><body><div class="header">我是顶部导航栏</div><div class="content"><div class="sk">秒杀模块</div></div><div class="backtop"><img src="./images/close2.png" alt=""><a href="javascript:;"></a></div><script>const sk = document.querySelector('.sk')const header = document.querySelector('.header')//1.页面滚动事件window.addEventListener('scroll', function () {//当页面滚动到 秒杀模块的时候,就改变头部的top值//页面被卷去的头部 >= 秒杀模块的位置  offsetTopconst n = document.documentElement.scrollTop// if (n >= sk.offsetTop) {//   header.style.top = 0// } else {//   header.style.top = '-80px'// }header.style.top = n >= sk.offsetTop ? 0 : '-80px'})</script></body></html>

实现bilibili点击小滑块移动效果:

<style>.line {transition: all .3s;}</style><script>//1.获取父元素 添加事件委托const list = document.querySelector('.tabs-list')const line = document.querySelector('.line')//2.给a注册事件list.addEventListener('click', function (e) {//判断点击的是aif (e.target.tagName === 'A') {// console.log(11)//得到当前链接的  offsetLeft// console.log(e.target.offsetLeft)//让line 盒子来进行移动line.style.transform = `translateX(${e.target.offsetLeft}px)`}})</script>
http://www.lryc.cn/news/390029.html

相关文章:

  • C语言中的基础指针操作
  • LabVIEW环境下OCR文字识别的实现策略与挑战解析
  • 中英双语介绍美国的州:堪萨斯州(Kansas)
  • 信息收集---端口服务信息收集
  • Python知识点背诵手册,超详细知识梳理
  • 【Pytorch实用教程】如何在多个GPU上使用分布式数据并行进行训练模型
  • PIL,OpenCV,Pytorch处理图像时的通道顺序(颜色,长宽深)
  • 经纬恒润亮相2024世界智能产业博览会
  • Python序列化和反序列化
  • Stream toArray 好过collect
  • qt/c++/mysql教务管理系统
  • Echarts公共方法
  • 鸿蒙学习(二)
  • 企业机构营销目前106短信群发还有用吗?此文告诉你该如何抉择!
  • DJYGUI AI低代码图形编程开发平台:开启嵌入式软件图形编程新纪元
  • 为什么不能在foreach中删除元素
  • python学习-tuple及str
  • Python深度理解系列之【排序算法——冒泡排序】
  • 边界框在目标检测中的作用与应用
  • linux 环境报错:Peer reports incompatible or unsupported protocol version
  • 深入解析:Java爬虫的本质是什么?
  • 【Matlab 六自由度机器人】机器人动力学之推导拉格朗日方程(附MATLAB机器人动力学拉格朗日方程推导代码)
  • 线下生鲜蔬果店做小程序有什么方法
  • 几种linux开机自启脚本的方法
  • Qt开发笔记:Qt3D三维开发笔记(一):Qt3D三维开发基础概念介绍
  • Firewalld 防火墙基础
  • 针对 Windows 10 的功能更新,版本 22H2 - 错误 0xc1900204
  • goframe框架规范限制(but it should be named with “Res“ suffix like “XxxRes“)
  • 格式化选NTFS还是exFAT 格式化NTFS后Mac不能用怎么办 移动硬盘格式化ntfs和exfat的区别
  • 中国桥梁空间分布数据