Home Web Front-end HTML Tutorial 纯CSS实现单击锚点平滑切换的效果_html/css_WEB-ITnose

纯CSS实现单击锚点平滑切换的效果_html/css_WEB-ITnose

Jun 24, 2016 am 11:30 AM

纯CSS实现单击锚点平滑切换的效果

22.50注:貌似自己错的有点离谱了o(╯□╰)o 大家先别看了
有时想实现一个功能,比如想让页面不出现滚动条,但是却能装下更多的东西,这个时候我们会对某个容器设置overflow: hidden,然后通过锚点来实现单击切换页面(其实只是控制显示的内容)的效果。但是通过锚点来跳到指定内容的不足之处在于,没有什么过渡效果,很突兀的就跳到了目标点。当然,也可以通过JS或者jQuery来实现自己想要的动态切换效果,这一类的文章一搜一大堆,这里就不赘述了。

但是在一些个人主页中,比如github的个人主页(只是主页而已,也就是username.github.io)上会禁止JS脚本的运行,这个时候如果还是想通过单击锚点平滑切换的功能的话,应该怎么办呢?

原谅我是一个前端初学者,傻想了很久也想不到什么办法,然后就百度谷歌了。

后来找到了一些方法,比如用radio配合他的check属性来实现,文章在这里,关于这个方法,慕课上也有专门的教程,大家也可以去看看。

但是可不可以不插入radio就实现对应的效果呢? 答案是有的。

那就是利用:target伪类,这个选择器可以选择当前活动的锚点,那么利用这个属性我们就可以实现自己想要的效果了。

代码如下:

<a href="#tag1">Tag1</a><a href="#tag2">Tag2</a><a href="#tag3">Tag3</a><div id="container">  <div id="tag1"></div>  <div id="tag2"></div>  <div id="tag3"></div></div>
Copy after login

div{  width: 100px;  height: 100px;  border-radius: 50%;}#container{  margin-top: 10px;  outline: 1px solid red;  position: relative;}#tag1,#tag2,#tag3{  transition: all ease-out .2s;  position: absolute;  top:0  left:0;  right:0;  bottom:0;  opacity: 0;  }#tag1{  background: #f66;  opacity: 1;  z-index: 1;}#tag2{  background: orange;}#tag3{  background: gray;}#container>div:target{  z-index: 1000;  opacity: 1;}
Copy after login

查看DEMO

上面的例子是通过绝对定位配合z-index和透明度来实现的动态效果。先将所有的元素的opacity设为0,然后为了默认显示出来第一个元素,所以第一个元素的opacity为1,z-index为1。然后单击就可以实现动态切换了。

其实我最先尝试的是通过给#container设置overflow: hidden;, 为它的子元素设置height:100%;,然后给target设置margin-top为负的百分比值来实现相应效果的,毕竟比较喜欢这种从下往上冒或者从上往下掉的感觉嘛。。。但是在实现过程中遇到了一点小问题,那就是当Tag2和Tag3相互切换的时候,居然只会移动到一半就不再动了,如果把overflow: hidden去掉,显示的就是自己期望的效果。一个有问题的DEMO

后来自己想到了解决的办法,那就是给container包裹一个div,然后把overflow: hidden设置在这个元素身上,再把container的overflow去掉,设置高度为它子元素的高度,这样就能显示正常了。 DEMO在这里.

虽然找到了解决办法,但是对于问题DEMO 里的这种怪异行为真的不理解,如果各位谁知道的话,还请告知,谢谢。

参见:
stackoverflow - 纯CSS滚动效果

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 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
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

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.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

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: 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.

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.

See all articles