仅当上一页是主页时才运行 jQuery



基本上主页具有JSFiddle开头的CSS,我希望内页仅在主页之后访问时才动画化为新的CSS类。

<div class="container">
  <div>
    Thank you for your assistance!
  </div>
</div>
<style>
.container {
    padding: 100px 0;
    background: #ccc;
    text-align: center;
}
.container div {
    background: #eaeaea;
    width: 200px;
    height: 200px;
    margin:0 auto;
    text-indent: -9999px;
}
.container.inner,
.container.inner div {
    transition: all 0.75s ease-in;
}
.container.inner {
    padding: 10px 0;
}
.container.inner div {
    width: 100px;
    height: 100px;
}
</style>
<script>
jQuery( document ).ready(function() {
    setTimeout(function() {
        if ( jQuery(".home").length == 0  ) {
            jQuery('.container').addClass('inner');
        }
    }, 500 );
});
</script>
我想我需要使用一两个cookie来确定上一页

是否是主页,如果上一页是内页,则需要使用另一个cookie来确定无延迟或动画地应用CSS。

document.referrer

你想要看的:

if(document.referrer.indexOf('home')){
   // apply the addClass here then.
}

注意:
document.referrer仅包含访问的最后一页。

我最终使用 PHP 来确定文档引用。谢谢@Jai为我指出正确的方向。我将更改CSS的jQuery放在一个单独的文件中,每当引用器为"index.php"或"/"时,我都会调用该js文件

这是我使用的确切代码(在WordPress函数上.php)

$homePagePaths = array (
  '/index.php',
  '/'
);
$parts = parse_url($_SERVER['HTTP_REFERER']);
if (empty($parts['path']) || in_array($parts['path'], $homePagePaths)) 
  wp_enqueue_script( 'inner-animation', 
  get_stylesheet_directory_uri() . '/js/inner-animation.js', 
  array('jquery'), '', true )
;

最新更新