简体   繁体   中英

($(window).scrollTop() > not working on IOS

I am using below JS code to fix header at the top for mobile only. means if screen is scrolled for 80px css classes will be replaced. working on android and PC but no luck on Ios. any suggestions?

$(window).scroll(function () {
if ($(window).width() < 768) {
    if ($(window).scrollTop() > 80) {
        $('.navbar-right').addClass('custom-fixed-top');
    }
    else{
        $('.navbar-right').removeClass('custom-fixed-top');
    }
}
});

HTML CODE

<nav class="navbar navbar-default">
<div class="mid-container">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed hidden-xs" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#"><img src="images/logo.png" class="img-responsive" alt="logo"></a>
        </div>

        <div class="navbar-collapse collapse in" id="bs-example-navbar-collapse-1" aria-expanded="true" style="">
            <ul class="nav navbar-nav navbar-right">
                <li class="hidden-xs"><a class="hdr-orng-btn" href="#">受付時間</a></li>
                <li class="hidden-xs"><a class="" href="#">10:00~19:00</a></li>
                <li><a class="num" href="tel:03-0000-0000">TEL 03-0000-0000</a></li>
                <li><a class="hdr-grn-btn" href="#contact_form">お問い合わせ</a></li>
            </ul>
        </div>
    </div>
</div>

.navbar-nav.custom-fixed-top{
position: fixed;
top: 0;
width: 100%;
height: auto;
padding: 0 15px 15px;
margin: 0;
z-index: 2;
background-color:#fff;
left:0;

} this is the code.

Ok this might seem far fetched but...

Try add the following to your CSS in your html tag if they're on IOS:

cursor: pointer;

Maybe can be your if verification of viewport screen width with 768, verify if you use some screen above that size. I try replicate your code here, but $(window).scrollTop() works fancy here for me.

Image with your code on google chrome

Try changing this;

if ($(window).scrollTop() > 80) {...

To this;

if ($(body).scrollTop() > 80) {...

If that then works on ios, then that's the issue, so to cover both devices/browsers implement something like this;

var scrollentity = $('html,body');
if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) 
{ scrollentity = $('body') }
else  { scrollentity = $('html,body') }
scrollentity .scrollTop() > 80) {...

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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