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

纯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>

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;}

查看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
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
How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

HTML's Purpose: Enabling Web Browsers to Display ContentHTML's Purpose: Enabling Web Browsers to Display ContentMay 03, 2025 am 12:03 AM

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

Why are HTML tags important for web development?Why are HTML tags important for web development?May 02, 2025 am 12:03 AM

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

Explain the importance of using consistent coding style for HTML tags and attributes.Explain the importance of using consistent coding style for HTML tags and attributes.May 01, 2025 am 12:01 AM

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

How to implement multi-project carousel in Bootstrap 4?How to implement multi-project carousel in Bootstrap 4?Apr 30, 2025 pm 03:24 PM

Solution to implement multi-project carousel in Bootstrap4 Implementing multi-project carousel in Bootstrap4 is not an easy task. Although Bootstrap...

How does deepseek official website achieve the effect of penetrating mouse scroll event?How does deepseek official website achieve the effect of penetrating mouse scroll event?Apr 30, 2025 pm 03:21 PM

How to achieve the effect of mouse scrolling event penetration? When we browse the web, we often encounter some special interaction designs. For example, on deepseek official website, �...

See all articles

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

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software