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

PHPz
Release: 2023-10-24 09:02:25
Original
1251 people have browsed it

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:




  

Copy after login

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%);
  }
}
Copy after login

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!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!