<img> 标记视差图像而不是背景图像:在 CSS 中附加



我试图在标签上产生视差效果。但它不起作用。http://prntscr.com/rbq4ha我需要使图像像链接图像一样具有视差。请帮帮我。

使用容器元素并将背景图像添加到具有特定高度的容器中。然后使用background-attachment: fixed来创建实际的视差效果。其他背景属性用于完美地居中和缩放图像/这里有一个例子:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.parallax {
/* The image used */
background-image: url("https://miro.medium.com/max/2046/1*ZJuc_2TsDElu6Gtg_KxiEg.png");
/* Set a specific height */
min-height: 500px; 
/* Create the parallax scrolling effect */
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
</style>
</head>
<body>
<p>Scroll Up and Down this page to see the parallax scrolling effect.</p>
<div class="parallax"></div>
<div style="height:1000px;background-color:red;font-size:36px">
Scroll Up and Down this page to see the parallax scrolling effect.
This div is just here to enable scrolling.
Tip: Try to remove the background-attachment property to remove the scrolling effect.
</div>
</body>
</html>

这里有一个简单的教程如何创建视差滚动。