简体   繁体   English

为什么我的Javascript在iOS上无法使用?

[英]Why would my Javascript break on iOS?

I have this code on jsFiddle that is implemented and works just fine on desktop browsers and works, to a point, on mobile devices. 我在jsFiddle上有这段代码,该代码已实现,并且在桌面浏览器上可以正常运行,并且在某种程度上可以在移动设备上运行。 http://jsfiddle.net/heufT/ http://jsfiddle.net/heufT/

What my code is doing 我的代码在做什么

If the screen is larger than 960px wide (or thereabouts) a normal horizontal navigation will be displayed, however if the screen is less than 960px wide the navigation becomes apart of a button, which when clicked reveals the same links in a vertical menu instead. 如果屏幕的宽度大于960px(或其附近),则将显示正常的水平导航,但是,如果屏幕的宽度小于960px,则导航将变为按钮的一部分,单击该按钮会在垂直菜单中显示相同的链接。 When you scroll the page, the header will shrink to a smaller height and if you go back to the top the header goes back to the same height as before. 滚动页面时,标题将缩小到较小的高度,如果返回顶部,标题将恢复为与以前相同的高度。 There is also a .load script that will ensure this all happens even when you resize your browser (mainly for use on desktop). 还有一个.load脚本,即使您调整浏览器的大小(主要在桌面上使用),也可以确保所有这些操作均会发生。

jQuery(document).ready(function($){

// prepend menu icon
$('nav').prepend('<div id="menu-icon"></div>');

/* toggle nav */
$("#menu-icon").on("click", function(){
    $("ul#prim").slideToggle();
    $(this).toggleClass("active");
});

});

// Navigation resize event on scroll
$(document).scroll(function(){

if($(window).width()>959){
      $("ul#prim").addClass("adjTop");
}

if($(window).width()<958){
      $("ul#prim").removeClass("adjTop");
}

if ($(this).scrollTop()>105){
    // animate fixed div to small size:
    $('header').stop().animate({ height: 90 },50, 'linear');
    $('ul#prim.adjTop').stop().animate({ top: '50%', 'margin-top': 18 },50, 'linear');
    $('ul#prim').stop().animate({ top: 62 },50, 'linear');
    $("img.logo").fadeOut();
    $("img.bao_logo").fadeIn(1000);
} else {
    //  animate fixed div to original size
    $('header').stop().animate({ height: 175 },50, 'linear');
    $('ul#prim.adjTop').stop().animate({ top: '50%', 'margin-top': 18 },50, 'linear');  
    $('ul#prim').stop().animate({ top: 105 },50, 'linear');
    $("img.logo").fadeIn(1000);
    $("img.bao_logo").hide();
}

});

$(window).resize(function() {
if($(window).width()>959){
    $("ul#prim").addClass("adjTop");
}

if($(window).width()<958){
      $("ul#prim").removeClass("adjTop");
}

// Showreel
$(window).resize(function(){
    // Resize video to fix aspect ratio when window resizes
    // Only do this if video is currently visible
    if ($('#showreel').height()!=0){
        $('#showreel').height(($('#showreel').width()/16)*9);
    }
});
});

(function($){

// Custom scrollbars for work feature on homepage
$(window).load(function(){
    $(".scroll-pane").mCustomScrollbar({
            horizontalScroll:true,
            mouseWheel: false
    });
});

})(jQuery);​

The problem 问题

The one thing I have been noticing though on iPad and iPhone especially is that the JS will work but then when you pinch/zoom Javascript completely breaks and the navigation button doesn't work nor does the shrinking/growing of my header. 我特别注意到的一件事是,在iPad和iPhone上,JS可以正常使用,但是当您捏/缩放Javascript时,它完全损坏了,导航按钮不起作用,我的标题也没有缩小/增长。

I have tried disabling pinch/zoom using the meta viewport tag which obviously stops the user from zooming in to the page but even when you at least try to pinch/zoom and it doesn't do anything, i've noticed the JS still breaks and nothing works. 我尝试使用meta视口标记禁用收缩/缩放,这显然会阻止用户放大页面,但是即使您至少尝试收缩/缩放并且它什么也没做,我注意到JS仍然坏了没有任何效果。

Has anyone got any pointers? 有人有指针吗? Is there any errors in my code that would casue this? 我的代码中是否有任何错误会导致这种情况? Am I missing anything? 我有什么想念的吗?

Ok so after testing for a couple of days I ended up revisiting the meta viewport tag and added a maximum-scale attribute combined with the user scalable attribute and it cured the problem. 好的,经过几天的测试,我最终重新访问了meta视口标签,并添加了与用户可扩展属性结合的maximum-scale属性,从而解决了该问题。 Not sure if this is the right way to go about things as the user is prevented from pinch/zooming and therefore impacts usability but this is a short term solution none the less. 由于无法防止用户捏捏/缩放并因此影响可用性,因此不确定这是否是正确的处理方法,但这仍然是短期解决方案。

My viewport tag now looks like so: 我的视口标签现在看起来像这样:

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM