【发布时间】:2015-10-14 21:13:47
【问题描述】:
我目前正在使用fullPage.js 作为website I'm creating。请注意,当您滚动时,它会在 URL 上附加一个哈希(即#feature1)。我想根据该哈希更改图像或背景图像
这是在我的<body>标签上方初始化fullPage.js
$(document).ready(function() {
$('#fullpage').fullpage({
anchors: ['welcome', 'feature1', 'feature2', 'feature3', 'feature4', 'feature5', 'feature6'],
sectionsColor: ['transparent', '#1BBC9B', '#7E8F7C', '#C63D0F', '#1BBC9B', '#7E8F7C'],
navigation: true,
navigationPosition: 'left',
});
});
我正在使用这个 javascript 来尝试更改图像,尽管我很确定 document.write 是错误的。
$(document).ready(function() {
// Which anchor is being used?
switch(window.location.hash) {
case "#feature1":
document.write('<img class="screen" src="img/bg-feature-2.jpg">');
break;
case "#feature2":
document.write('<img class="screen" src="img/bg-feature-1.jpg">');
break;
}
});
如果我滚动到#feature1 或#feature2,它什么也不做。但是,如果我直接在浏览器中键入带有哈希值的 URL,它只会显示图像而不会显示其他任何内容。我该如何解决这个问题?
【问题讨论】:
-
整页插件提供
afterSlideLoad、onSlideLeave等多种回调方法。利用这些将是在现有代码中实现您想要的内容的最佳方式。只需查看documentation 的示例。 -
保罗,你看到我的回答了吗?
标签: javascript jquery scroll fullpage.js