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
Simulating Mouse MovementSimulating Mouse MovementApr 22, 2025 am 11:45 AM

If you've ever had to display an interactive animation during a live talk or a class, then you may know that it's not always easy to interact with your slides

Powering Search With Astro Actions and Fuse.jsPowering Search With Astro Actions and Fuse.jsApr 22, 2025 am 11:41 AM

With Astro, we can generate most of our site during our build, but have a small bit of server-side code that can handle search functionality using something like Fuse.js. In this demo, we’ll use Fuse to search through a set of personal “bookmarks” th

Undefined: The Third Boolean ValueUndefined: The Third Boolean ValueApr 22, 2025 am 11:38 AM

I wanted to implement a notification message in one of my projects, similar to what you’d see in Google Docs while a document is saving. In other words, a

In Defense of the Ternary StatementIn Defense of the Ternary StatementApr 22, 2025 am 11:25 AM

Some months ago I was on Hacker News (as one does) and I ran across a (now deleted) article about not using if statements. If you’re new to this idea (like I

Using the Web Speech API for Multilingual TranslationsUsing the Web Speech API for Multilingual TranslationsApr 22, 2025 am 11:23 AM

Since the early days of science fiction, we have fantasized about machines that talk to us. Today it is commonplace. Even so, the technology for making

Jetpack Gutenberg BlocksJetpack Gutenberg BlocksApr 22, 2025 am 11:20 AM

I remember when Gutenberg was released into core, because I was at WordCamp US that day. A number of months have gone by now, so I imagine more and more of us

Creating a Reusable Pagination Component in VueCreating a Reusable Pagination Component in VueApr 22, 2025 am 11:17 AM

The idea behind most of web applications is to fetch data from the database and present it to the user in the best possible way. When we deal with data there

Using 'box shadows' and clip-path togetherUsing 'box shadows' and clip-path togetherApr 22, 2025 am 11:13 AM

Let's do a little step-by-step of a situation where you can't quite do what seems to make sense, but you can still get it done with CSS trickery. In this

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

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

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.