2014-09-26 93 views
0

我该如何正确检查当前页面的URL,以及该URL是否在数组中为页面设置动画效果?以下是我到目前为止,但每个页面动画不只是在我的数组中。基于url的动画页面

此脚本加载在页脚的每个页面上。

我相信这很简单,但我有限的JS知识。谢谢!

var pageArray = new Array("/how-it-works.php", "/themethodology.php", "/thecommunity.php","/thetools.php"); 
    var currrentPage = window.location.pathname; 

    if (jQuery.inArray(jQuery('currrentPage'), pageArray)){ 

    $('html, body').animate({ 
      scrollTop: $("#pageheader_cont").offset().top 
     }, 1000); 

    } 
+0

你是什么意思*“每页动画”* ..?一个网址打开一页,右.. ..? 0.o – 2014-09-26 07:11:03

+0

对不起,我不是更清楚。点击加载我的导航中的每一页。但页面,但它不应该动画,除非该页面在数组中。 – Denoteone 2014-09-26 07:41:34

+0

请更新问题并在其中添加额外信息,而不是在评论中。那么,你在所有页面上都有相同的脚本? – 2014-09-26 08:08:02

回答

0

请尝试以下操作。我相信它是你想要的。

$(function(){ 

    var pages = new Array("how-it-works.php", "themethodology.php", "thecommunity.php","thetools.php"); 

    var script = window.location.href.match(/[^\/]+$/)[0]; // Get current script's name 

    if (pages.indexOf(script) != -1){ 

     $('html, body').animate({ 

      scrollTop: $("#pageheader_cont").offset().top 

     }, 1000); 
    } 

}); 
+0

我不确定为什么,但每当我进入新页面时,动画仍会触发,无论它是哪个页面。只是为了测试我从数组中删除了所有的值,它仍然是动画的。我错过了什么吗? – Denoteone 2014-09-26 08:12:42

+0

你说得对。如果条件错了。我改变了它,我认为它现在起作用。 – tliokos 2014-09-26 08:17:57

+0

啊哈,做到了。感谢您的帮助。它有帮助吗? – Denoteone 2014-09-26 08:27:15