What is the best method of re-rendering a web page on orientation change?

后端 未结 8 732
臣服心动
臣服心动 2020-11-28 06:43

I have a fluid CSS layout which is rendering badly on an iphone when I change the orientation. (It looks fine when it is refreshed).

I am using the code below to ref

相关标签:
8条回答
  • 2020-11-28 07:27

    I had a similar problem and this is how I fixed it with jQuery.

    In my case the styles were not changing correctly for my <nav> element. I used a modified version of kidkaos77's code combined with $.get() to only load a certain part of the page:

    $(window).bind("resize",function() {
        //on resize reload only nav & replace
        $.get("index.php" +  ' nav', function(data) {
            $("nav").replaceWith($(data).find("nav"));
        });
    });
    

    With this method you don't have to reload the entire page, but you would have to specify exactly which elements are being affected by the orientation change, which in your case it seems like you just need one div.

    0 讨论(0)
  • 2020-11-28 07:28

    Try something like this:

    $(function(){
    
        changeOrientation(window.orientation == 0 ? "portrait" : "landscape");
    
        $('body').bind('orientationchange',function(event){
            changeOrientation(event.orientation)
        });
    
        function changeOrientation(ori){
            $("#orientation").removeClass('portrait landscape');
            $("#orientation").addClass(ori);
        }     
    });
    
    0 讨论(0)
提交回复
热议问题