我如何在使用fullpage.js时改变基于URL哈希的图像
How can I change an image based on URL hash when using fullpage.js?
我目前正在使用fullPage.js为我正在创建的网站。请注意,当您滚动时,它如何向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,它将只显示图像而不显示其他内容。我该如何解决这个问题?
正如@Brian Ray指出的那样,你应该使用fullpage.js回调。
您可以使用afterLoad
或onLeave
的部分和afterSlideLoad
或onSlideLoad
的幻灯片。
$('#fullpage').fullpage({
anchors: ['firstPage', 'secondPage', 'thirdPage', 'fourthPage', 'lastPage'],
afterLoad: function (anchorLink, index) {
var loadedSection = $(this);
//using anchorLink
if (anchorLink == 'secondSlide') {
alert("Section 2 ended loading");
}
}
});
虽然在你的情况下,我会直接在CSS中使用fullpage.js添加到body
元素的fp-viewing-section-slide
类型的CSS类。
看看这个视频教程,它解释了如何使用它。
例如:body.fp-viewing-firstPage #myImage{
background: url('demo.gif');
}
body.fp-viewing-secondPage #myImage{
background: url('otherImage.gif');
}
您需要将您的页面绑定到hashchange
事件:
$(function(){
// Bind an event to window.onhashchange that, when the hash changes, gets the
// hash and adds the class "selected" to any matching nav link.
$(window).hashchange( function(){
var hash = location.hash;
// Change the image link using the following code:
// $(".screen").attr("src", "img/bg-feature-2.jpg");
switch(window.location.hash) {
case "#feature1":
$(".screen").attr("src", "img/bg-feature-2.jpg");
break;
case "#feature2":
$(".screen").attr("src", "img/bg-feature-1.jpg");
break;
}
})
// Since the event is only triggered when the hash changes, we need to trigger
// the event now, to handle the hash the page may have loaded with.
$(window).hashchange();
});
我使用The hash is blank作为引用
您正在寻找hashchange
事件- Mozilla有相关文档。
但是基本上可以这样使用:
window.addEventListener("hashchange", function() {
// called whenever the hash changes
// put your switch / case here.
}, false);
您的$(document).ready()
方法的问题是,它只在页面加载后运行一次(实际上,一旦文档准备好)。而不是以后。url哈希值的变化并不是加载什么东西,它只是将您跳转到同一文档的不同"片段"。
相关文章:
- angular ui路由器:创建可扩展的、不改变url的布局状态
- 另一个“;如何在不离开网页的情况下改变URL”;
- 如何改变url而不改变主干页面
- 正则表达式javascript改变url中的参数值
- 如何改变URL(哈希),当用户滚动或点击链接与jquery历史插件
- 如何用javascript改变URL的格式
- Angular的$compile正在改变URL
- 获取网页URL(移动到其他页面而不改变URL)
- AngularJS:改变URL,但不改变浏览器后退和前进按钮的控制器或模板加载
- 当改变url参数时,React路由器不会重新加载组件
- 流星路由器.即使页面转换,也不要改变Url
- 基于点击改变url
- JQuery Fancybox -试图改变url的图像一旦点击
- 有没有办法改变url是iframe
- 改变url的位置
- 改变url加载到javascript文件的根目录
- 方法:如何改变URL而不重新加载页面和索引整个URL(锚#部分)
- AngularJS ui-bootstrap打开模态窗口而不改变url
- 不改变URL的React Router
- 如何改变URl链接在我的Javascript