使用Javascript和PHP在不同的页面上以不同的方式设置标题动画
Header animated differently on separate pages using Javascript and PHP
我有一个header.php文件,它被调用到我的所有页面中,其中有:
<header <?php if (is_page('Home')) { echo 'id="home-masthead" class="site-header" role="banner"'; } else { echo 'id="masthead" class="site-header" role="banner"'; } ?>>
这意味着,如果我在主页上,我可以在CSS中使用#home-masthead
为该页面的页眉设置样式,而要为所有其他页面设置样式,我可以仅使用#masthead
为页眉设置样式。
我的问题是,即使php为我的主页提供标题id="#home-masthead" and all other pages
id="mashead"`,并且样式在这些页面上也有效。当我试图在任何页面上动画化标题id,但主页不起作用。
这是我的CSS:
#masthead {
height: 100px;
transition: all 0.5s;
/* other styles here */
}
.animate#masthead {
height: 75px;
}
和我的JS:
// Header Shrink
$(function() {
var shrinkHeader = 125;
$(window).scroll(function() {
var scroll = getCurrentScroll();
if ( scroll >= shrinkHeader ) {
$('#home-masthead, #masthead, #logo, #list-container').addClass('animate'); // header-animate
}
else {
$('#home-masthead, #masthead, #logo, #list-container').removeClass('animate'); // header-animate
}
});
function getCurrentScroll() {
return window.pageYOffset || document.documentElement.scrollTop;
}
});
tl;drJS头动画只在主页上工作,而不能在其他页面上工作,这可能是因为php代码更改了主页以外页面的id。
试试这个代码。console.log应该出现在开发工具的控制台选项卡上
if ( scroll >= shrinkHeader ) {
console.log("entered if");
$('#home-masthead, #masthead, #logo, #list-container').addClass('animate'); // header-animate
}
else {
console.log("entered else");
$('#home-masthead, #masthead, #logo, #list-container').removeClass('animate'); // header-animate
}
相关文章:
- angularjs路线过渡如何以交互方式设置动画
- 使用JQuery以不同的方式设置每个
- 样式
- Javascript或浏览器以不同的方式设置位置
- 离子/角度日期输入使用时刻以编程方式设置
- 以安全的方式设置nodeJS Transform流的编码
- UIWebView - 以编程方式设置默认日期
- 我如何在 jQuery 中以编程方式设置输入文本框的值
- 尝试以编程方式设置焦点不适用于 Firefox
- 如何以图像居中的方式设置网站的位置
- Sitecore SPEAK UI 以编程方式设置组合框的选定项
- Select2 以编程方式设置搜索词并打开包含相关结果的下拉列表(无 ajax 调用)
- 在 JavaScript 中以编程方式设置构造函数参数
- 如何以不同的方式设置每行/行的每个最后一个元素的样式
- 如何以正确的方式设置选择标签以具有“5”的大小
- 如何使用 JavaScript 以编程方式设置 input:focus 样式
- 以编程方式设置渐变
- SVG 动画在以编程方式设置样式时有效,但在仅使用样式表时无效
- 如何以编程方式设置 chrome 和 Firefox 的主页
- 以编程方式设置 jqGrid 列中编辑选项的“只读”属性
- 在 C# 中以编程方式设置和获取浏览器窗口坐标值 (x,y)