ios - 根据我的媒体查询禁用 JS

标签 ios css media-queries javascript

我在我的网站(在 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 :

http://jsfiddle.net/64Dde/

非常感谢您的帮助,

最佳答案

这可能是您的答案: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/

相关文章:

ios - Swift:对象数组搜索

javascript - 有没有办法使用 CSS3/Canvas 来 flex/弧形文本

html - 响应式设计 : Why isn't css responding to mobile screen size?

html - 如何为 chrome 和 firefox 设置 @media (min-width) 和 (max-width)

ios - 在 Swift 中使用 Backendless 自定义模型类

iphone - CGContext 使用两个不透明的 UIColor 绘制

ios - 如何在 ODOO 中的特定客户(res.partner)下创建任务(project.task)?

javascript - 如何设置图像路径的样式

javascript - 自动调整 iFrame 大小

html - 我在带显示的媒体查询中显示一个 div 吗?