html - 我的移动网站在 iphone 上加载不一致

标签 html css mobile scroll overflow

我正在开发一个移动网站模板,但我不确定为什么它有时会在我的 iPhone 上正确加载,而有时它只会加载正文(我将正文设置为海军蓝的空白背景)。我的代码中的一些警报按预期出现,但其余容器和 header 有时根本不加载,但有时它可以完美加载而无需更改任何代码。这是不一致的,很难弄清楚。但它始终可以毫无错误地加载到我的 table ​​面上。 关于我的代码: 我加载了一个提供给所有设备的 css 文件。在此文件中,标题是绝对位置,container_wrapper 或主体上没有设置高度。 在一个 js 文件中,我调用了一个方法来检查浏览器是否支持溢出:滚动。 如果它确实支持,那么文档会提示 true(目前仅用于调试)。警报对于不同的手机来说是完全正确的。 如果它确实支持溢出滚动,那么我会调用一个方法来添加一些额外的 css 样式,即 body 和 container_wrapper 的高度并将标题设置为固定位置。还有一个名为 scrollable 的类可以滚动(使用 css 样式)到容器 div 和侧边菜单 div。如果有人对加载不一致的原因有任何想法,我将不胜感激。我附上了3张图片。未正确加载时的第一个空白页和正确加载时的第二个空白页。

谢谢你...

这是我的 CSS。

body{
margin-top: 0px;  
font-family:Arial,Helvetica,sans-serif;
width: 100%;
overflow: auto;
background: #120F3A;
}


#container_wrapper{
position: absolute;
left: 0px;
top: 0px;
display: block;
margin: 0 auto;
width: 100%;
background: #120F3A;
overflow: auto;
max-width: 100%; 
z-index: 2;
-webkit-transition: .3s;
-moz-transition: .3s;
-o-transition: .3s;
transition: .3s;  
}


#container{
position: relative; 
overflow: hidden;
z-index: 2;
}


/********************************************/
/*Styling for header and drop down menu. Used on every page */
header{
width: 100%;
}

header nav{
width: 100%;
}
#headernav {  
width: 100%;  
position:absolute;
top: 0px;
left: 0px;
z-index: 10;
background: #000;
padding-bottom: 2px;
-webkit-transition: .3s; /* Safari */
transition: .3s;
}


#swipe_menu_right{
display: block;
width: 80%;
max-width: 80%;
position:absolute;
background: #120F3A;
top: 0px;
left: 20%;
padding: 0px 0px 0px 0px;
z-index:1;
overflow: auto;
-webkit-transition: .3s; 
-moz-transition: .3s;
-o-transition: .3s;
transition: .3s;  
}

#header_right{
display: none;
z-index: 600;
width: 25%;
position: fixed;
overflow: hidden;
left: -5%;

}

.scrollable{
overflow: scroll;
-webkit-transition-property: top, bottom;
-moz-transition-property: top, bottom;
-o-transition-property: top, bottom;
transition-property: top, bottom;

-webkit-transition-duration: .2s, .2s;
-moz-transition-duration: .2s, .2s;
-o-transition-duration: .2s, .2s;
transition-duration: .2s, .2s;

-webkit-transition-timing-function: linear, linear;
-moz-transition-timing-function: linear, linear;
-o-transition-timing-function: linear, linear;
transition-timing-function: linear, linear;
-webkit-overflow-scrolling: touch;
}

#menu_list{
position: relative; 
overflow: hidden;

}
#menu_list .accordian_nav{
margin-top: 0px;
padding-top: 0px;
}
#menu_list .accordian_nav ul{
width: 94%;
margin-left: 6%;

}
#menu_list .menu_level_1{
width: 95%;
margin: 0px 0% 0px 0%;
border: none;
-webkit-border-radius: 0px;
-moz-border-radius: 0px;
-o-border-radius: 0px;
border-radius: 0px;
}

和一个名为 js_functions 的 Js 文件,我在加载 css 后加载它。

$(document).ready(function(){
    try{
    if((sessionStorage.getItem("canScroll") != 'false') && (sessionStorage.getItem("canScroll") != 'true')){
        if(canOverflowScroll()){
            alert('true');
            changeCssOnDocReady();
            sessionStorage.setItem("canScroll", "true");    
        }else{
            alert('false');
            sessionStorage.setItem("canScroll", "false");   
        }
    }else if(sessionStorage.getItem("canScroll") == 'true'){
        changeCssOnDocReady();
    }

    }catch(error){
            alert(error.message);
    }
});

function changeCssOnDocReady(){
    alert(' csstrue');
    $('body').css({
        'height': '100%'
    });
    $('#container_wrapper').css({
        'height': '100%'
    });
    $('#swipe_menu_right').css({
        'height': '100%'
    });
    $("#headernav").css({
        'position': 'fixed'
    });
    alert('wrapper height' + $('#container_wrapper').height());
}
function canOverflowScroll() {
    return (
        // Features-first. iOS5 overflow scrolling property check - no UA needed here. thanks Apple :)
        "WebkitOverflowScrolling" in window.document.documentElement.style ||
        // Touch events aren't supported and screen width is greater than X
        // ...basically, this is a loose "desktop browser" check. 
        // It may wrongly opt-in very large tablets with no touch support.
        window.screen.width > 1200 ||
        // Hang on to your hats.
        // Whitelist some popular, overflow-supporting mobile browsers for now and the future
        // These browsers are known to get overlow support right, but give us no way of detecting it.
        (function() {
            var ua = window.navigator.userAgent,
                // Webkit crosses platforms, and the browsers on our list run at least version 534
                webkit = ua.match(/AppleWebKit\/([0-9]+)/),
                wkversion = webkit && webkit[1],
                wkLte534 = webkit && wkversion >= 534;

            return (
                /* Android 3+ with webkit gte 534
                ~: Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13 */
                ua.match(/Android ([0-9]+)/) && RegExp.$1 >= 3 && wkLte534 ||
                /* Windows Phone 
                ~: Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 920) */
                ua.match(/IEMobile\/(\d\d).\d+/) && RegExp.$1 >= 10 ||
                /* Blackberry 7+ with webkit gte 534
                ~: Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0 Mobile Safari/534.11+ */
                ua.match(/ Version\/([0-9]+)/) && RegExp.$1 >= 0 && window.blackberry && wkLte534 ||
                /* Blackberry Playbook with webkit gte 534
                ~: Mozilla/5.0 (PlayBook; U; RIM Tablet OS 1.0.0; en-US) AppleWebKit/534.8+ (KHTML, like Gecko) Version/0.0.1 Safari/534.8+ */
                ua.indexOf(/PlayBook/) > -1 && RegExp.$1 >= 0 && wkLte534 ||
                /* Firefox Mobile (Fennec) 4 and up
                ~: Mozilla/5.0 (Mobile; rv:15.0) Gecko/15.0 Firefox/15.0 */
                ua.match(/Firefox\/([0-9]+)/) && RegExp.$1 >= 4 ||
                /* WebOS 3 and up (TouchPad too)
                ~: Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; en-US) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.48 Safari/534.6 TouchPad/1.0 */
                ua.match(/wOSBrowser\/([0-9]+)/) && RegExp.$1 >= 233 && wkLte534 ||
                /* Nokia Browser N8
                ~: Mozilla/5.0 (Symbian/3; Series60/5.2 NokiaN8-00/012.002; Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/533.4 (KHTML, like Gecko) NokiaBrowser/7.3.0 Mobile Safari/533.4 3gpp-gba 
                ~: Note: the N9 doesn't have native overflow with one-finger touch. wtf */
                ua.match(/NokiaBrowser\/([0-9\.]+)/) && parseFloat(RegExp.$1) === 7.3 && webkit && wkversion >= 533
            );
        })()
    );
}

我的 html 代码:

<body ontouchstart="">
    <div id="header_right">
        <span class="more_button_swipe"><i class="fa fa-navicon"></i></span>
    </div>
    <div id="swipe_menu_right" class="scrollable">  
        <div id="menu_list">
            <div class="header_side_menu"><h3>Menu</h3></div>
            <ul class="accordian_nav">
                <li><span class="menu_level_1 block"><i class="fa fa-headphones icon_width"></i>Audio<i class="fa fa-plus-square plus_minus_arrow"></i></span>      
                            <ul>        
                                    <li><a class="menu_level_2 ableton_music_link" href="music-production.php#">Music Production<i class="fa fa-chevron-right"></i></a></li>
                                    <li><a class="menu_level_2 dj_techniques_link" href="techniques.php">Techniques Cert.<i class="fa fa-chevron-right"></i></a></li>
                                    <li><a class="menu_level_2" href="#">DJ Techniques Cert.<i class="fa fa-chevron-right"></i></a></li>        
                            </ul>
                        </li>
                <li><span class="menu_level_1 block"><i class="fa fa-gamepad icon_width"></i>Games<i class="fa fa-plus-square plus_minus_arrow"></i></span>
                    <ul>
                                    <li><a class="menu_level_2" href="music-production.php#">3D Game<i class="fa fa-chevron-right"></i></a></li>
                            </ul>

                </li>
                <li><span class="menu_level_1 block"><i class="fa fa-laptop icon_width"></i>I.T.<i class="fa fa-plus-square plus_minus_arrow"></i></span>
                    <ul>
                                    <li><a class="menu_level_2" href="music-production.php#">D Programming<i class="fa fa-chevron-right"></i></a></li>
                                    <li><a class="menu_level_2" href="music-production.php#">D C Proamming<i class="fa fa-chevron-right"></i></a></li>
                                </ul>   
                </li>
                        <li><a class="menu_level_1 vouchers_link"><i class="fa fa-gift icon_width"></i>Voucher Deals<i class="fa fa-chevron-right"></i></a></li>
                <li><a class="menu_level_1 about_link" href="about-us.php#"><i class="fa fa-info-circle icon_width"></i>About Us<i class="fa fa-chevron-right"></i></a></li>
                <li><a class="menu_level_1 contact_link" href="contact-us.php#"><i class="fa fa-envelope icon_width"></i>Contact Us<i class="fa fa-chevron-right"></i></a></li>
            </ul>

        </div>
    </div>
    <div id="container_wrapper" class="scrollable">
    <header>
        <nav>
            <ul id="headernav">  
                    <li><a href="index.php#"><i class="fa fa-home"></i></a></li>
                    <li><a href="location.php#"><i class="fa fa-map-marker"></i></a></li>  
                    <li><a href="tel:[00353872237667]"><i class="fa fa-phone"></i></a></li>  
                    <li><span class="more_button_swipe"><i class="fa fa-navicon"></i></span></li>     
                </ul>               
        </nav>
    </header>
        <div id="container">
            <div id="swipe_sidebar_right"></div>
            <div id="logo_wrapper">
                <img class="logo" src="images/logo-virtu.jpg" height="65" width="150" alt="Logo"/>
                <h2>Contact</h2>
            </div>
            <div id="content">
                <div id="background_grey">  
                    <section id="form_area" class="content_box">
                    </section><!--End of form_area-->
                </div><!--End of background_grey-->
            </div><!--End of content-->
        </div><!--End of container-->   
</body>

最佳答案

我现在可以使用了。 我已经决定改变周围的东西,所以加载的默认 css 页面具有支持 overflow:scroll 的较新型号手机所需的样式。因此,我将主体、container_wrapper 和侧边菜单的高度设置为 100%,而不是在收到对检查是否溢出的肯定响应后更改它:支持滚动... 相反,我检查是否不支持溢出滚动,然后如果不支持我动态添加一个新的样式表,其中 body 和 container_wrapper 的高度为 height: auto;而不是高度:100%。似乎是这样,即从 height:100% 更改为 height: auto 正确显示它,而不是我从没有高度开始并在支持溢出滚动时添加 100% 高度的方式*支持..它是处理加载速度,但是当我这样做时,即使加载第二个样式表有延迟,它在加载后仍然正确显示。 所以我将以下代码添加到我的头上:

<link rel="stylesheet" type="text/css" href="css/main_style_new_phones.css"/>
<script>

if(!canOverflowScroll()){
var lnk=document.createElement('link');
lnk.href='css/main_style_old_phones.css';
lnk.rel='stylesheet';
lnk.type='text/css';
(document.head||document.documentElement).appendChild(lnk);
}

这是 main_style_new_phones.css 中的 css

body{
margin-top: 0px;  /*moves #container right up to top of page so no gap*/
font-family:Arial,Helvetica,sans-serif;
width: 100%;
overflow: auto;
z-index: 1;
height: 100%;
}

#container_wrapper{
position: absolute;
left: 0px;
top: 0px;
display: block;
margin: 0 auto;
width: 100%;
background: #120F3A;
overflow: auto;
max-width: 100%;  /*same width as full logo image width so that when on full screen the background color of the container doesn't spread across*/ 
z-index: 4;
-webkit-transition: .3s; /* Android 2.1+, Chrome 1-25, iOS 3.2-6.1, Safari 3.2-6  */
-moz-transition: .3s;
-o-transition: .3s;
transition: .3s;  /* Chrome 26, Firefox 16+, iOS 7+, IE 10+, Opera, Safari 6.1+  */
height: 100%;
}
#headernav {  
width: 100%;  
position:fixed;
top: 0px;
left: 0px;
z-index: 10;
background: #000;
padding-bottom: 2px;
-webkit-transition: .3s; /* Safari */
transition: .3s;
}

在 main_style_old_phones.css 中不支持溢出滚动的手机的 css:

body{
    height: auto;
    }
    #container_wrapper{
    height: auto;
    }

    #headernav {    
    position:absolute;
    }

    #swipe_menu_right{
    height: auto;
    }

关于html - 我的移动网站在 iphone 上加载不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28482408/

相关文章:

html - 使用CSS对象适合:contain on image leaves blank spaces around it

css - 在移动设备上将静态导航栏更改为静态

testing - 在桌面浏览器中测试移动网站

javascript - 从 NativeScript 列表中删除项目时如何设置动画

javascript - 在图像上实现平移窗口?

javascript - 使用 JavaScript 禁用鼠标选择

javascript - 如何将下拉列表中的三 Angular 形图标更改为html中的自定义图标

javascript - 如何将我的默认滚动条更改为自定义对象标签?

css - 如何将 css 文件与 Slim 模板引擎链接?

jquery - 菜单未作为列表打开