Table of Contents
光标效果不见了?
js里几个获取元素宽高属性的比较
Home Web Front-end HTML Tutorial 前端碎语(6)_html/css_WEB-ITnose

前端碎语(6)_html/css_WEB-ITnose

Jun 24, 2016 am 11:25 AM

光标效果不见了?

在页面里,屏幕上光标的样式我们可以用css的’cursor’属性进行定义。一般来讲,只要光标hover到指定的元素上面其样式就会按我们指定的进行显示,但是如果我们指定的元素被其他元素“遮住了”呢?来看看下面这个demo:

demo

可以看到,虽然我们给div1自定义了光标样式,但在被div2盖住的那部分,我们预设的效果就没有了,或者说这个效果是不能“穿透”div2的。这也就提示我们,当我们的一些交互需要我们自定义光标样式的时候(比如拖动),一定要注意元素间的堆叠顺序,否则很有可能会出现你移动到某个位置上时你的光标效果突然消失的现象。

至于css本身是如何决定元素间堆叠顺序的,这还是个比较复杂的问题,具体地可以看看 张鑫旭这篇文章 。

js里几个获取元素宽高属性的比较

offsetWidth 、 clientWidth 、 scrollWidth 、 offsetHeight 、 clientHeight 、 scrollHeight 这几个属性的特点一直傻傻分不清,这次就好好把他们弄清楚吧。先看demo:

demo

先分析一下div1和待滚动条的div2两个普通元素的情况,在不同浏览器下(不考虑IE8-)运行上面这个demo后,在控制台可以看到各浏览器的结果都是一样的:

可以得出结论:

  • offsetWidth 和 offsetHeight 属性:数值对应的是元素的可视宽高,含元素本身宽高、padding、(有滚动条时)滚动条、border。

  • clientHeight 和 clientWidth 属性:数值对应的是元素的内容加padding所占据的视觉面积,有滚动条时还要加上滚动条,不含border。

  • scrollHeight 和 scrollWidth 属性:由div1的结果可见,没有滚动条时执行结果和 clientHeight 、 clientWidth 一样。有滚动条的情况则复杂一点,上面这个例子中div2的内容足够大、能产生滚动,故此时结果为 content 的大小;而如果把 content 这个div调小、直到不能产生滚动时结果应该是和 clientHeight 、 clientWidth 一样的。

上面的结论只是针对页面普通元素,如果使用这几个属性的是整个页面(html元素,这里用 document.documentElement 访问),那输出的情况就复杂了,没有明显的规律,找的不同的资料说法也不太相同、并不能解释我测试的结果。所以这里只讲一些我测试之后比较固定的结果,仅供参考:

  • 首先 clientHeight 和 clientWidth 的行为普通元素的不太一样,在所有浏览器里基本指的都是页面视口的大小。

  • scrollHeight 和 scrollWidth 仍要分有没有滚动,有滚动时指的是整个页面内容的大小;没滚动时在chrome下指视口的大小、IE和FF下则是和 offsetWidth 、 offsetHeight 一样。

  • offsetWidth 与 offsetHeight 属性在chrome、FF、IE11下都和原来一样指整个元素的可视宽高。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

The Role of HTML: Structuring Web Content The Role of HTML: Structuring Web Content Apr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

See all articles