运行 jQuery 仅当上一页为首页时
Run jQuery only when previous page was homepage
基本上主页有 CSS 和 JSFiddle 开头,我希望内页动画到他们的新 CSS class 只有在主页。
<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 functions.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 )
;
基本上主页有 CSS 和 JSFiddle 开头,我希望内页动画到他们的新 CSS class 只有在主页。
<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 functions.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 )
;