简体   繁体   English

jQuery在iOS设备上无法正常工作

[英]jquery don't work properly on iOS device

I'm testing my web application on all browsers. 我正在所有浏览器上测试我的Web应用程序。

On Android it works perfectly, but on iOS seems jquery not work properly. 在Android上,它工作正常,但在iOS上,jQuery似乎无法正常工作。 I need the height and the width of my <canvas> change if the device is in portrait or landscape mode, but on iOS i need refresh the page for make it work. 如果设备处于纵向或横向模式,我需要更改<canvas>的高度和宽度,但是在iOS上,我需要刷新页面以使其正常工作。

This problem is very strange. 这个问题很奇怪。 If i change the iOS device to landscape or portrait, it show the correct alert but the size of my <canvas> don't change. 如果我将iOS设备更改为横向或纵向,它会显示正确的警报,但<canvas>的大小不会更改。

This is my code: 这是我的代码:

<style type="text/css">
  .wrapper {
  position: relative;
  width: 280px;
  height: 420px;
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.signature-pad {
  position: absolute;
  left: 0;
  top: 0;
  width: 280px;
  height: 420px;
}
</style>

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    if ( window.matchMedia("(orientation: landscape)").matches ) {  
        $(".wrapper").attr("width", '500');
        $('.wrapper').css('width','500px');
        $(".signature-pad").attr("width", '500');
        $('.signature-pad').css('width','500px');
        $(".wrapper").attr("height", '210');
        $('.wrapper').css('height','210px');
        $(".signature-pad").attr("height", '210');
        $('.signature-pad').css('height','210px');
    }else{
        $(".wrapper").attr("width", '280');
        $('.wrapper').css('width','280px');
        $(".signature-pad").attr("width", '280');
        $('.signature-pad').css('width','280px');
        $(".wrapper").attr("height", '420');
        $('.wrapper').css('height','420px');
        $(".signature-pad").attr("height", '420');
        $('.signature-pad').css('height','420px');
    }

    $( window ).on( "orientationchange", function( event ) {
    if ( window.matchMedia("(orientation: landscape)").matches ) {  
        alert('LANDSCAPE!!!!');
        $(".wrapper").attr("width", '280');
        $('.wrapper').css('width','280px');
        $(".signature-pad").attr("width", '280');
        $('.signature-pad').css('width','280px');
        $(".wrapper").attr("height", '420');
        $('.wrapper').css('height','420px');
        $(".signature-pad").attr("height", '420');
        $('.signature-pad').css('height','420px');
    }else{
        alert('PORTRAIT!!!!');
        $(".wrapper").attr("width", '500');
        $('.wrapper').css('width','500px');
        $(".signature-pad").attr("width", '500');
        $('.signature-pad').css('width','500px');
        $(".wrapper").attr("height", '210');
        $('.wrapper').css('height','210px');
        $(".signature-pad").attr("height", '210');
        $('.signature-pad').css('height','210px');
    }
    });

    var signaturePad = new SignaturePad(document.getElementById('signature-pad'), {
        backgroundColor: 'rgba(255, 255, 255, 0)',
        penColor: 'rgb(0, 0, 0)'
    });

    var cancelButton = document.getElementById('clear');
    cancelButton.addEventListener('click', function (event) {
        signaturePad.clear();
    });
});
</script>

<div class="wrapper" id="wrapper">
      <canvas id="signature-pad" class="signature-pad" width="280" height="420"></canvas>
</div>

Thank! 谢谢!

All these styling can be done easily with media query css. 所有这些样式都可以通过媒体查询CSS轻松完成。 Why do you do this with jquery? 为什么用jquery做到这一点? Responsive CSS is so much easier than maintaining it with jquery. 响应式CSS比使用jquery进行维护要容易得多。

Read more @ http://www.w3schools.com/css/css_rwd_mediaqueries.asp and many million tutorial available on the net. 阅读更多@ http://www.w3schools.com/css/css_rwd_mediaqueries.asp,以及网上提供的数以百万计的教程。

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

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