• 元素在网页或视口上位置的相关问题求解


    1、求元素在整张网页的高度和宽度
    可从document.documentElement(即<html>元素)或<body>元素上读取。

    //网页总高度
    Document.documentElement.offsetHeight
    Document.documentElement.scrollHeight
    Document.body.offsetHeight
    Document.body.scrollHeight

    由于html和body的宽度可能设置的不一样,因此从body上取会更保险些。。。

    2、求元素在视口的高度和宽度

    Window.innerHeight//包括滚动条
    Document.documentElement.clientHeight//不包括滚动条

    3、求某个网页元素在视口上的坐标,使用element.getBoundingClientRect方法获取

    //网页元素在视口上的横坐标
    Element.getBoundingClientRect().left
    //网页元素在视口上的纵坐标
    Element.getBoundingClientRect().top

    4、求某个网页元素在网页上的坐标,使用视口坐标加上网页滚动距离。

    //元素在网页上的横坐标
    Element.getBoundingClientRect().left+document.documentElement.scrollLeft
    //元素在网页上的横坐标
    Element.getBoundingClientRect().top+document.documentElement.scrollTop

    5、网页元素本身的高度和宽度
    1)、通过offsetHeight和offsetWidth(width、padding和border)
    2)、通过clientHeight和clientWidth(width和padding)

  • 相关阅读:
    网站视频播放器JCPlayer
    常用Linux命令操作
    对词法分析器的了解
    实验二简化版C语言中文理解程序文法
    词法分析
    实验三有穷自动机的构造和识别
    组合数据类型练习,综合练习
    零基础初学者学习编译原理
    用python画五星红旗
    Android环境搭建和编写helloworld
  • 原文地址:https://www.cnblogs.com/MelodysBlog/p/10449469.html
Copyright © 2020-2023  润新知