使用 CSS 在一个 HTML 部分创建视差
Creating a parallax on one HTML section with CSS
我建立了一个网站,其中包含多个部分,但我想对其中一个部分应用视差效果。我搜索了很多并设法找到 this article,它描述了如何使用 CSS 实现视差效果。我的问题是我不是很了解它,无论我怎么努力,它似乎都不管用...
我认为我遇到问题的原因可能是我的 HTML 的结构,不一定是因为提供的代码不正确。谁能看出我哪里出错了?
请看我的JSFiddle或看下面:
.padding {display:block;height:200px;background:#ccc;}
/********** DEFINE THE STYLES FOR THE SEMINARS SECTION **********/
#seminars {position:relative;background:transparent;background-size:cover;height:485px;}
#seminars::after {content:'';display:block;position:absolute;background-image: linear-gradient(to bottom, blue 0%, red 100%);height:885px;width:100%;margin-top:-200px;z-index:-1;}
/* Define the seminar content */
#seminar_content {position:relative;float:left;width:600px;top:110px;left:100px;}
#seminar_content h1,
#seminar_content p
{color:#fff;}
#seminar_content h1 {font-size:33px;font-weight:normal;padding:0;margin:0 0 30px;}
#seminar_content p {font-size:16px;line-height:150%;}
<section class="padding"></section>
<section id="seminars">
<div class="mn_content">
<article id="seminar_content">
<h1>Discover lorem ipsum seminar</h1>
<p>This is Photoshop's version of Lorem Ipsum. Proin gravida nibh vel velit auctor aliquet. Aenean sollicitudin, lorem quis bibendum auctor, nisi elit consequat ipsum, nec sagittis sem nibh id elit.</p>
</article>
</div>
</section>
<section class="padding"></section>
我尝试过的代码如下:
perspective: 1px;
transform: translateZ(0);
我想成为视差的部分是#seminars::after
如果由于我的 html 结构而无法使用 CSS 实现,有人可以指出我正确的实现方向吗这与 javascript 或 jQuery?
你所说的视差是什么意思。让两个背景在不同的时间函数中移动 不能 仅通过 CSS 来实现。 jQuery 中有一千种不同的插件可以实现这种效果。一个示例是:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery Parallax Plugin Demo</title>
<base href="http://ianlunn.co.uk/plugins/jquery-parallax/" />
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript" src="scripts/jquery.parallax-1.1.3.js"></script>
<script type="text/javascript" src="scripts/jquery.localscroll-1.2.7-min.js"></script>
<script type="text/javascript" src="scripts/jquery.scrollTo-1.4.2-min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#nav').localScroll(800);
//.parallax(xPosition, speedFactor, outerHeight) options:
//xPosition - Horizontal position of the element
//inertia - speed to move relative to vertical scroll. Example: 0.1 is one tenth the speed of scrolling, 2 is twice the speed of scrolling
//outerHeight (true/false) - Whether or not jQuery should use it's outerHeight option to determine when a section is in the viewport
$('#intro').parallax("50%", 0.1);
$('#second').parallax("50%", 0.1);
$('.bg').parallax("50%", 0.4);
$('#third').parallax("50%", 0.3);
})
</script>
</head>
<body>
<ul id="nav">
<li><a href="#intro" title="Next Section"><img src="images/dot.png" alt="Link" /></a></li>
<li><a href="#second" title="Next Section"><img src="images/dot.png" alt="Link" /></a></li>
<li><a href="#third" title="Next Section"><img src="images/dot.png" alt="Link" /></a></li>
<li><a href="#fifth" title="Next Section"><img src="images/dot.png" alt="Link" /></a></li>
</ul>
<div id="intro">
<div class="story">
<div class="float-left">
<h2>(Almost) Static Background</h2>
<p>This section has a background that moves slightly slower than the user scrolls. This is achieved by changing the top position of the background for every pixel the page is scrolled.</p>
</div>
</div> <!--.story-->
</div> <!--#intro-->
<div id="second">
<div class="story"><div class="bg"></div>
<div class="float-right">
<h2>Multiple Backgrounds</h2>
<p>The multiple backgrounds applied to this section are moved in a similar way to the first section -- every time the user scrolls down the page by a pixel, the positions of the backgrounds are changed.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean nibh erat, sagittis sit amet congue at, aliquam eu libero. Integer molestie, turpis vel ultrices facilisis, nisi mauris sollicitudin mauris, volutpat elementum enim urna eget odio. Donec egestas aliquet facilisis. Nunc eu nunc eget neque ornare fringilla. Nam vel sodales lectus. Nulla in pellentesque eros. Donec ultricies, enim vitae varius cursus, risus mauris iaculis neque, euismod sollicitudin metus erat vitae sapien. Sed pulvinar.</p>
</div>
</div> <!--.story-->
</div> <!--#second-->
<div id="third">
<div class="story">
<div class="float-left">
<h2>What Happens When JavaScript is Disabled?</h2>
<p>The user gets a slap! Actually, all that jQuery does is moves the backgrounds relative to the position of the scrollbar. Without it, the backgrounds simply stay put and the user would never know they are missing out on the awesome! CSS2 does a good enough job to still make the effect look cool.</p>
</div>
</div> <!--.story-->
</div> <!--#third-->
<div id="fifth">
<div class="story">
<p>Check out my new plugin <a href="http://www.sequencejs.com" title="Sequence.js">Sequence.js</a> for parallax effects and a whole lot more!</p>
<h2>Ian Lunn</h2>
<ul>
<li><strong>Twitter</strong>: <a href="http://www.twitter.com/IanLunn" title="Follow Ian on Twitter">@IanLunn</a></li>
<li><strong>GitHub</strong>: <a href="http://www.github.com/IanLunn" title="Follow Ian on GitHub">IanLunn</a></li>
<li><strong>Website</strong>: <a href="http://www.ianlunn.co.uk/" title="Ian Lunn Design">www.ianlunn.co.uk</a></li>
</ul>
<p>This demo is based on the <a href="http://www.nikebetterworld.com" title="Nike Better World">Nikebetterworld.com</a> website.</p>
<h2>Credits</h2>
<p>This plugin makes use of some scripts and images made by others:</p>
<ul>
<li><a href="http://flesler.blogspot.com/2007/10/jqueryscrollto.html" title="jQuery ScrollTo">jQuery ScrollTo</a></li>
<li><a href="http://downloads.dvq.co.nz/" title="Background Textures">Wooden and Pyschedlic Background Textures</a></li>
<li><a href="http://www.sxc.hu/photo/931435" title="Trainers Image">Trainers Image</a></li>
<li><a href="http://www.sxc.hu/photo/1015485" title="Basketball Image">Basketball Image</a></li>
<li><a href="http://www.sxc.hu/photo/563767" title="Bottles Image">Bottles Image</a></li>
</ul>
</div> <!--.story-->
</div> <!--#fifth-->
</body>
</html>
纯CSS道
抱歉我之前的事情。是的,有办法。看看这个网站:pure-css-parallax-websites.
我建立了一个网站,其中包含多个部分,但我想对其中一个部分应用视差效果。我搜索了很多并设法找到 this article,它描述了如何使用 CSS 实现视差效果。我的问题是我不是很了解它,无论我怎么努力,它似乎都不管用...
我认为我遇到问题的原因可能是我的 HTML 的结构,不一定是因为提供的代码不正确。谁能看出我哪里出错了?
请看我的JSFiddle或看下面:
.padding {display:block;height:200px;background:#ccc;}
/********** DEFINE THE STYLES FOR THE SEMINARS SECTION **********/
#seminars {position:relative;background:transparent;background-size:cover;height:485px;}
#seminars::after {content:'';display:block;position:absolute;background-image: linear-gradient(to bottom, blue 0%, red 100%);height:885px;width:100%;margin-top:-200px;z-index:-1;}
/* Define the seminar content */
#seminar_content {position:relative;float:left;width:600px;top:110px;left:100px;}
#seminar_content h1,
#seminar_content p
{color:#fff;}
#seminar_content h1 {font-size:33px;font-weight:normal;padding:0;margin:0 0 30px;}
#seminar_content p {font-size:16px;line-height:150%;}
<section class="padding"></section>
<section id="seminars">
<div class="mn_content">
<article id="seminar_content">
<h1>Discover lorem ipsum seminar</h1>
<p>This is Photoshop's version of Lorem Ipsum. Proin gravida nibh vel velit auctor aliquet. Aenean sollicitudin, lorem quis bibendum auctor, nisi elit consequat ipsum, nec sagittis sem nibh id elit.</p>
</article>
</div>
</section>
<section class="padding"></section>
我尝试过的代码如下:
perspective: 1px;
transform: translateZ(0);
我想成为视差的部分是#seminars::after
如果由于我的 html 结构而无法使用 CSS 实现,有人可以指出我正确的实现方向吗这与 javascript 或 jQuery?
你所说的视差是什么意思。让两个背景在不同的时间函数中移动 不能 仅通过 CSS 来实现。 jQuery 中有一千种不同的插件可以实现这种效果。一个示例是:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery Parallax Plugin Demo</title>
<base href="http://ianlunn.co.uk/plugins/jquery-parallax/" />
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript" src="scripts/jquery.parallax-1.1.3.js"></script>
<script type="text/javascript" src="scripts/jquery.localscroll-1.2.7-min.js"></script>
<script type="text/javascript" src="scripts/jquery.scrollTo-1.4.2-min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#nav').localScroll(800);
//.parallax(xPosition, speedFactor, outerHeight) options:
//xPosition - Horizontal position of the element
//inertia - speed to move relative to vertical scroll. Example: 0.1 is one tenth the speed of scrolling, 2 is twice the speed of scrolling
//outerHeight (true/false) - Whether or not jQuery should use it's outerHeight option to determine when a section is in the viewport
$('#intro').parallax("50%", 0.1);
$('#second').parallax("50%", 0.1);
$('.bg').parallax("50%", 0.4);
$('#third').parallax("50%", 0.3);
})
</script>
</head>
<body>
<ul id="nav">
<li><a href="#intro" title="Next Section"><img src="images/dot.png" alt="Link" /></a></li>
<li><a href="#second" title="Next Section"><img src="images/dot.png" alt="Link" /></a></li>
<li><a href="#third" title="Next Section"><img src="images/dot.png" alt="Link" /></a></li>
<li><a href="#fifth" title="Next Section"><img src="images/dot.png" alt="Link" /></a></li>
</ul>
<div id="intro">
<div class="story">
<div class="float-left">
<h2>(Almost) Static Background</h2>
<p>This section has a background that moves slightly slower than the user scrolls. This is achieved by changing the top position of the background for every pixel the page is scrolled.</p>
</div>
</div> <!--.story-->
</div> <!--#intro-->
<div id="second">
<div class="story"><div class="bg"></div>
<div class="float-right">
<h2>Multiple Backgrounds</h2>
<p>The multiple backgrounds applied to this section are moved in a similar way to the first section -- every time the user scrolls down the page by a pixel, the positions of the backgrounds are changed.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean nibh erat, sagittis sit amet congue at, aliquam eu libero. Integer molestie, turpis vel ultrices facilisis, nisi mauris sollicitudin mauris, volutpat elementum enim urna eget odio. Donec egestas aliquet facilisis. Nunc eu nunc eget neque ornare fringilla. Nam vel sodales lectus. Nulla in pellentesque eros. Donec ultricies, enim vitae varius cursus, risus mauris iaculis neque, euismod sollicitudin metus erat vitae sapien. Sed pulvinar.</p>
</div>
</div> <!--.story-->
</div> <!--#second-->
<div id="third">
<div class="story">
<div class="float-left">
<h2>What Happens When JavaScript is Disabled?</h2>
<p>The user gets a slap! Actually, all that jQuery does is moves the backgrounds relative to the position of the scrollbar. Without it, the backgrounds simply stay put and the user would never know they are missing out on the awesome! CSS2 does a good enough job to still make the effect look cool.</p>
</div>
</div> <!--.story-->
</div> <!--#third-->
<div id="fifth">
<div class="story">
<p>Check out my new plugin <a href="http://www.sequencejs.com" title="Sequence.js">Sequence.js</a> for parallax effects and a whole lot more!</p>
<h2>Ian Lunn</h2>
<ul>
<li><strong>Twitter</strong>: <a href="http://www.twitter.com/IanLunn" title="Follow Ian on Twitter">@IanLunn</a></li>
<li><strong>GitHub</strong>: <a href="http://www.github.com/IanLunn" title="Follow Ian on GitHub">IanLunn</a></li>
<li><strong>Website</strong>: <a href="http://www.ianlunn.co.uk/" title="Ian Lunn Design">www.ianlunn.co.uk</a></li>
</ul>
<p>This demo is based on the <a href="http://www.nikebetterworld.com" title="Nike Better World">Nikebetterworld.com</a> website.</p>
<h2>Credits</h2>
<p>This plugin makes use of some scripts and images made by others:</p>
<ul>
<li><a href="http://flesler.blogspot.com/2007/10/jqueryscrollto.html" title="jQuery ScrollTo">jQuery ScrollTo</a></li>
<li><a href="http://downloads.dvq.co.nz/" title="Background Textures">Wooden and Pyschedlic Background Textures</a></li>
<li><a href="http://www.sxc.hu/photo/931435" title="Trainers Image">Trainers Image</a></li>
<li><a href="http://www.sxc.hu/photo/1015485" title="Basketball Image">Basketball Image</a></li>
<li><a href="http://www.sxc.hu/photo/563767" title="Bottles Image">Bottles Image</a></li>
</ul>
</div> <!--.story-->
</div> <!--#fifth-->
</body>
</html>
纯CSS道
抱歉我之前的事情。是的,有办法。看看这个网站:pure-css-parallax-websites.