我有一个带有简单PHP导航的网站(使用$_GET-Variables)。
导航链接的HTML代码(它们向右浮动):
<a href="index.php?main=test1" class="menuelement">Test 1</a>
<a href="index.php?main=test2" class="menuelement">Test 2</a>
<a href="index.php?main=test2" class="menuelement">Test 3</a>
包含页面的简单PHP代码块:
if(!isset($_GET['main'])) {
$page = "home";
} else {
$page = $_GET['main'];
}
include($page.".php");
两个代码库都在index.php.上
菜单元素使用jQuerys.animate()进行动画化,如下所示(代码介于$(document).ready之间):
$('.menuelement').hover(function () {
if ($(this).hasClass('clicked') == false) {
$(this).stop(true, false).animate({
'padding-right': '80px'
}, 900, 'easeOutBounce');
}
},
function () {
if ($(this).hasClass('clicked') == false) {
$(this).stop(true, false).animate({
'padding-right': '0px'
}, 900, 'easeOutBounce');
}
}
).click(function() {
if ($(this).hasClass('clicked') == true) {
//Don't do anything.
} else {
$('a.clicked').animate({
'padding-right': '0px'
}, 900, 'easeOutBounce');
$('a.clicked').removeClass('clicked');
$(this).addClass('clicked');
$(this).stop(true, false).animate({
'padding-right': '80px'
}, 900, 'easeOutBounce');
}
});
发生的情况是,悬停的菜单元素向左滑动(记住:它是向右浮动的),当鼠标移出时,它会向后滑动。单击时,它会停留在原地,并获取一个类来标记它已被单击。如果你点击另一个菜单元素,之前点击的菜单元素会向后滑动,而新的菜单元素则会保持滑动状态。所有这些都是用addClass、removeClass和一些if-else语句完成的。。。非常简单。如果链接的a-Tags没有任何链接目标,例如只有hash符号(),一切都很好。
但是一旦链接有了目标(例如),当点击菜单元素时,jQuery动画就会再次启动,因为index.php有点被重新加载了。结果是,单击的菜单元素不会一直滑出。
我知道我可以(而且我已经做了)用e.load()将一些内容加载到一个div中。但我不想在SEO和其他方面这样做。
有没有任何方法可以使用PHP导航和jQuerys奇妙的.animate(),而不需要每次单击链接时都"重新加载"动画?也许有一些PHP或jQuery提示我不知道。
谢谢你的帮助!
您可以使用event.prventDefault()来阻止浏览器跟随链接,并在动画完成后使用animate中的完整回调函数来加载链接到的页面。