search
HomeWeb Front-endCSS TutorialHow to achieve smooth scrolling background effect on web pages through pure CSS

How to achieve smooth scrolling background effect on web pages through pure CSS

Oct 24, 2023 am 09:02 AM
scrollpure csssmoothbackground effect

How to achieve smooth scrolling background effect on web pages through pure CSS

How to achieve smooth scrolling background effect of web pages through pure CSS

Background is a very important part of web design, which can enhance the visual effect and user experience of the page. Traditional web page backgrounds are usually static, but by using pure CSS technology, we can achieve a smooth scrolling background effect, thereby adding a more dynamic and vivid visual effect to the web page. In this article, we will introduce how to use CSS to achieve a smooth scrolling background effect, and provide specific code examples.

1. Preparation

Before we start to achieve the smooth scrolling background effect, we need to prepare some basic HTML and CSS code. First, we need to create a basic HTML file and then introduce a CSS file into it so that we can write code in the CSS file to achieve the smooth scrolling background effect. Here is a basic HTML structure example:

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
  <div class="background"></div>
  <div class="content">
    <!-- 网页内容 -->
  </div>
</body>
</html>

In the above example, we have created a background layer (div.background) and a content layer (div.content ), the background layer will be used to achieve a smooth scrolling background effect, and the content layer will be used to place other web content.

2. Achieve smooth scrolling background effect

Next, let us write CSS code to achieve smooth scrolling background effect. The following is a code example:

body {
  margin: 0;
  padding: 0;
  overflow: hidden;
}

.background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh; /* 占满整个可视区域 */
  background-image: url(background.jpg); /* 背景图片路径 */
  background-size: cover; /* 背景图片适应屏幕尺寸 */
  background-position: center; /* 背景图片居中显示 */
  z-index: -1; /* 确保背景层在内容层下方 */
  animation: scrollBackground 30s linear infinite; /* 启用滚动动画 */
}

@keyframes scrollBackground {
  0% {
    transform: translateY(-50%);
  }
  100% {
    transform: translateY(50%);
  }
}

In the above CSS code, we first set the overflow attribute of the body element of the web page to hidden , to hide the scrollbar. We then fixed the background layer in the viewport using position: fixed, width: 100% and height: 100vh to set the size of the background layer to and The background-image and background-size attributes are used to specify the path and scaling method of the background image, and the background-position attribute is used to control the background image. s position. Finally, we use the animation attribute to apply the scroll animation to the background layer, where scrollBackground is the animation name we defined, 30s represents the time to scroll once, linear means the animation transition is linear, infinite means the animation plays in an infinite loop. In the @keyframes rules of animation, we use the translateY function of the transform attribute to achieve the scrolling effect in the vertical direction.

3. Try the effect

After completing the above code, we can open the HTML file in the browser to view the smooth scrolling background effect. As you scroll the page, the background layer will appear in a smooth scrolling manner, giving you the feeling of being there.

Summary

Through pure CSS technology, we can easily achieve the smooth scrolling background effect of web pages. With just a few lines of CSS code, you can add a dynamic and vivid visual effect to your web page. I hope the content of this article can help you, and I wish you can achieve a satisfactory smooth scrolling background effect!

The above is the detailed content of How to achieve smooth scrolling background effect on web pages through pure CSS. For more information, please follow other related articles on the PHP Chinese website!

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
@keyframes vs CSS Transitions: What is the difference?@keyframes vs CSS Transitions: What is the difference?May 14, 2025 am 12:01 AM

@keyframesandCSSTransitionsdifferincomplexity:@keyframesallowsfordetailedanimationsequences,whileCSSTransitionshandlesimplestatechanges.UseCSSTransitionsforhovereffectslikebuttoncolorchanges,and@keyframesforintricateanimationslikerotatingspinners.

Using Pages CMS for Static Site Content ManagementUsing Pages CMS for Static Site Content ManagementMay 13, 2025 am 09:24 AM

I know, I know: there are a ton of content management system options available, and while I've tested several, none have really been the one, y'know? Weird pricing models, difficult customization, some even end up becoming a whole &

The Ultimate Guide to Linking CSS Files in HTMLThe Ultimate Guide to Linking CSS Files in HTMLMay 13, 2025 am 12:02 AM

Linking CSS files to HTML can be achieved by using elements in part of HTML. 1) Use tags to link local CSS files. 2) Multiple CSS files can be implemented by adding multiple tags. 3) External CSS files use absolute URL links, such as. 4) Ensure the correct use of file paths and CSS file loading order, and optimize performance can use CSS preprocessor to merge files.

CSS Flexbox vs Grid: a comprehensive reviewCSS Flexbox vs Grid: a comprehensive reviewMay 12, 2025 am 12:01 AM

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

How to Include CSS Files: Methods and Best PracticesHow to Include CSS Files: Methods and Best PracticesMay 11, 2025 am 12:02 AM

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Flexbox vs Grid: should I learn them both?Flexbox vs Grid: should I learn them both?May 10, 2025 am 12:01 AM

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)May 09, 2025 am 09:57 AM

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSS Animations: Is it hard to create them?CSS Animations: Is it hard to create them?May 09, 2025 am 12:03 AM

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

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 Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor