我在我的网站(在 wordpress 上)上使用一个 JS 脚本来在滚动时固定在顶部标题上。 我现在正在处理移动设备和 iPad 的媒体查询,因为存在一些问题...
我想在移动设备和 iPad 上禁用此 JS,以便我的标题保持相对。 我找不到办法做到这一点, 谁能帮忙?
这是我的 JS 代码:
$(function () {
// Check the initial Poistion of the Sticky Header
var stickyHeaderTop = $('#stickyheader').offset().top;
$(window).scroll(function () {
if ($(window).scrollTop() === stickyHeaderTop+1) {
$('#stickyheader').hide();
console.log('p');
}
if ($(window).scrollTop() > stickyHeaderTop) {
$('#stickyheader').fadeIn(500).css('position','fixed');
$('#stickyalias').css('display', 'block');
var mT = $('#stickyheader').css('height');
$('#stickyheader').next('.post').css('marginTop', mT);
}else{
$('#stickyheader').css({
position: 'relative',
});
$('#stickyheader').next('.post').css('marginTop', '0');
}
});
});
和我的CSS:
#header {background-color: #f8f8f8;top: 0;padding-top: 20px;}
#stickyheader { width: 100%;padding-top:10px;top: 0;z-index: 1000;padding-bottom: 10px;line-height: 24px;position:relative;background-color: #f8f8f8;}
#unstickyheader {height:auto;}
我的媒体查询 CSS:
/* Media queries!
-------------------------------------------------------------------------------*/
/*IPAD*/
@media screen and (max-device-width : 1024px) {
}
/*IPHONE*/
@media screen and (max-device-width : 568px) and (min-device-width : 320px) {
#menu-menu {list-style:none;font-size: 35px;font-weight: 700}
}
最后是 JS fiddle :
非常感谢您的帮助,
最佳答案
这可能是您的答案:https://github.com/paulirish/matchMedia.js/
JavaScript 的 CSS 媒体查询。
与您的代码相关的示例:
if (matchMedia("media query here").matches) {
window.onscroll = ...
}
关于ios - 根据我的媒体查询禁用 JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21735549/