我是HTML5,CSS3和JavaScript的新手,所以请不要苛刻。
我正在尝试在我的网页上创建一个视差效果,但菜单栏会一直滚动效果,一旦到达顶部,它应该位于屏幕顶部的固定位置。我尝试将HTML文件链接到CSS文件和JavaScript文件。当这不起作用时,我尝试在脚本标签中添加样式标签和JavaScript中的CSS代码,结果是一样的。我正在使用Dreamweaver CC。
所以我在网上找到了一个提供完美效果的例子,但是它使用JavaScript,每当我尝试使用JavaScript时它都无法运行。有人可以告诉我,我做错了。我从https://codepen.io/nodws/pen/ugFcC得到了例子。我真的想把这种风格融入我的页面,但如果我甚至无法让这个例子起作用,那么我就迷失了。
我尝试将HTML文件链接到CSS文件和JavaScript文件,将相应的代码放在相应的文件中。当这不起作用时,我尝试在脚本标签中添加样式标签和JavaScript中的CSS代码,结果是一样的。我正在使用Dreamweaver CC。
//Based on the Scroller function from @sallar
var $content = $('header .content'),
$blur = $('header .overlay'),
wHeight = $(window).height();
$(window).on('resize', function() {
wHeight = $(window).height();
});
window.requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
function Scroller() {
this.latestKnownScrollY = 0;
this.ticking = false;
}
Scroller.prototype = {
init: function() {
window.addEventListener('scroll', this.onScroll.bind(this), false);
$blur.css('background-image', $('header:first-of-type').css('background-image'));
},
onScroll: function() {
this.latestKnownScrollY = window.scrollY;
this.requestTick();
},
requestTick: function() {
if (!this.ticking) {
window.requestAnimFrame(this.update.bind(this));
}
this.ticking = true;
},
update: function() {
var currentScrollY = this.latestKnownScrollY;
this.ticking = false;
var slowScroll = currentScrollY / 2,
blurScroll = currentScrollY * 2,
opaScroll = 1.4 - currentScrollY / 400;
if (currentScrollY > wHeight)
$('nav').css('position', 'fixed');
else
$('nav').css('position', 'absolute');
$content.css({
'transform': 'translateY(' + slowScroll + 'px)',
'-moz-transform': 'translateY(' + slowScroll + 'px)',
'-webkit-transform': 'translateY(' + slowScroll + 'px)',
'opacity': opaScroll
});
$blur.css({
'opacity': blurScroll / wHeight
});
}
};
var scroller = new Scroller();
scroller.init();
@import url(https://fonts.googleapis.com/css?family=Lato);
html,
body {
padding: 0;
margin: 0;
height: 100%;
}
html {
font: 1em/1.5 "Lato", sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-rendering: optimizelegibility;
}
body {
font-size: 1.3em;
}
header {
height: 100%;
position: relative;
overflow: hidden;
background: url(https://unsplash.imgix.net/45/ZLSw0SXxThSrkXRIiCdT_DSC_0345.jpg?q=75&w=1080&h=1080&fit=max&fm=jpg&auto=format&s=857f07b76abac23a7fb7161cc7b12a46) center no-repeat;
/* Image Credit: Unsplash.me */
background-size: cover;
}
header .content {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 1;
}
header h1,
header h2 {
margin: 0;
}
header h2 {
text-transform: uppercase;
margin-top: -.5em;
}
header hgroup {
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
display: inline-block;
text-align: center;
position: absolute;
top: 50%;
left: 50%;
color: #fff;
border: 5px solid #fff;
padding: .5em 3em;
background-color: rgba(0, 0, 0, 0.2);
z-index: 2;
}
header .overlay {
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
background: #333 center no-repeat;
background-size: cover;
z-index: 0;
opacity: 0;
-webkit-filter: blur(4px);
}
img {
margin-right: 20px
}
.site {
padding: 20em 0;
text-align: center;
background-color: #efefef;
font-size: .8em;
color: #444;
position: relative
}
.site a {
color: #666;
text-decoration: none;
}
.site a:hover {
color: #222;
}
.site nav {
position: absolute;
top: 0;
left: 0;
background: #222;
width: 100%
}
.site nav a {
padding: 10px 30px;
font-size: 1.3em;
display: inline-block
}
.site nav a:hover {
background: #333;
color: #fff
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
<header>
<div class="content">
<hgroup>
<h1>LOGO</h1>
<i>slogan</i>
</hgroup>
</div>
<div class="overlay"></div>
</header>
<section class="site">
<nav>
<a href="">Page</a>
<a href="">Page</a>
<a href="">Page</a>
<a href="">Page</a>
<a href="">Page</a>
</nav>
<blockquote>
<img src="http://d.gr-assets.com/authors/1397426898p5/203714.jpg" align="left">“I invented nothing new. I simply assembled the discoveries of other men behind whom were centuries of work. Had I worked fifty or ten or even five years before, I would
have failed. So it is with every new thing. Progress happens when all the factors that make for it are ready, and then it is inevitable. To teach that a comparatively few men are responsible for the greatest forward steps of mankind is the worst
sort of nonsense.” ― Henry Ford
</blockquote>
</section>
</body>
</html>
答案 0 :(得分:0)
您似乎缺少对jQuery的引用
//Based on the Scroller function from @sallar
var $content = $('header .content'),
$blur = $('header .overlay'),
wHeight = $(window).height();
$(window).on('resize', function() {
wHeight = $(window).height();
});
window.requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
function Scroller() {
this.latestKnownScrollY = 0;
this.ticking = false;
}
Scroller.prototype = {
init: function() {
window.addEventListener('scroll', this.onScroll.bind(this), false);
$blur.css('background-image', $('header:first-of-type').css('background-image'));
},
onScroll: function() {
this.latestKnownScrollY = window.scrollY;
this.requestTick();
},
requestTick: function() {
if (!this.ticking) {
window.requestAnimFrame(this.update.bind(this));
}
this.ticking = true;
},
update: function() {
var currentScrollY = this.latestKnownScrollY;
this.ticking = false;
var slowScroll = currentScrollY / 2,
blurScroll = currentScrollY * 2,
opaScroll = 1.4 - currentScrollY / 400;
if (currentScrollY > wHeight)
$('nav').css('position', 'fixed');
else
$('nav').css('position', 'absolute');
$content.css({
'transform': 'translateY(' + slowScroll + 'px)',
'-moz-transform': 'translateY(' + slowScroll + 'px)',
'-webkit-transform': 'translateY(' + slowScroll + 'px)',
'opacity': opaScroll
});
$blur.css({
'opacity': blurScroll / wHeight
});
}
};
var scroller = new Scroller();
scroller.init();
&#13;
@import url(https://fonts.googleapis.com/css?family=Lato);
html,
body {
padding: 0;
margin: 0;
height: 100%;
}
html {
font: 1em/1.5 "Lato", sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-rendering: optimizelegibility;
}
body {
font-size: 1.3em;
}
header {
height: 100%;
position: relative;
overflow: hidden;
background: url(https://unsplash.imgix.net/45/ZLSw0SXxThSrkXRIiCdT_DSC_0345.jpg?q=75&w=1080&h=1080&fit=max&fm=jpg&auto=format&s=857f07b76abac23a7fb7161cc7b12a46) center no-repeat;
/* Image Credit: Unsplash.me */
background-size: cover;
}
header .content {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 1;
}
header h1,
header h2 {
margin: 0;
}
header h2 {
text-transform: uppercase;
margin-top: -.5em;
}
header hgroup {
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
display: inline-block;
text-align: center;
position: absolute;
top: 50%;
left: 50%;
color: #fff;
border: 5px solid #fff;
padding: .5em 3em;
background-color: rgba(0, 0, 0, 0.2);
z-index: 2;
}
header .overlay {
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
background: #333 center no-repeat;
background-size: cover;
z-index: 0;
opacity: 0;
-webkit-filter: blur(4px);
}
img {
margin-right: 20px
}
.site {
padding: 20em 0;
text-align: center;
background-color: #efefef;
font-size: .8em;
color: #444;
position: relative
}
.site a {
color: #666;
text-decoration: none;
}
.site a:hover {
color: #222;
}
.site nav {
position: absolute;
top: 0;
left: 0;
background: #222;
width: 100%
}
.site nav a {
padding: 10px 30px;
font-size: 1.3em;
display: inline-block
}
.site nav a:hover {
background: #333;
color: #fff
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
<header>
<div class="content">
<hgroup>
<h1>LOGO</h1>
<i>slogan</i>
</hgroup>
</div>
<div class="overlay"></div>
</header>
<section class="site">
<nav>
<a href="">Page</a>
<a href="">Page</a>
<a href="">Page</a>
<a href="">Page</a>
<a href="">Page</a>
</nav>
<blockquote>
<img src="http://d.gr-assets.com/authors/1397426898p5/203714.jpg" align="left">“I invented nothing new. I simply assembled the discoveries of other men behind whom were centuries of work. Had I worked fifty or ten or even five years before, I would
have failed. So it is with every new thing. Progress happens when all the factors that make for it are ready, and then it is inevitable. To teach that a comparatively few men are responsible for the greatest forward steps of mankind is the worst
sort of nonsense.” ― Henry Ford
</blockquote>
</section>
</body>
</html>
&#13;