How do I get scrollbars to show in Mobile Safari?

2019-01-17 11:46发布

问题:

The jQuery time-picker plugin that I wrote uses a div as the containing block for the list of times, and on Mobile Safari there are no scrollbars to indicate that there are more available times than are visible. I know about using two fingers to scroll within the div (on the iPad at least), but that only works if the user knows that there is more content to scroll to, and there's no indication that there is. So, my question: Has anyone been able to get scrollbars to show in Mobile Safari? How'd you do it?

回答1:

Assuming you are using iOS5.0 or later, I think you have to use the following:

-webkit-overflow-scrolling: auto (this is default style)

auto: One finger scrolling without momentum.

The other available style is

-webkit-overflow-scrolling: touch

touch: Native-style scrolling. Specifying this style has the effect of creating a staking context (like opacity, masks, and transforms).

Using touch mode, the scrollbar will be visible when the user touches and scrolls, but disappear when not in use. If you want to make it always visible, then this old post will help you:

::-webkit-scrollbar {
    -webkit-appearance: none;// you need to tweak this to make it available..
    width: 8px;
}

Another Piece of Code for Thumb by @BJMC:

::-webkit-scrollbar-thumb {
    border-radius: 4px;
    background-color: rgba(0,0,0,.5);
    box-shadow: 0 0 1px rgba(255,255,255,.5);
}

Original Source

Edit: with respect to this demo's behaviour, you should use jQuery because it will help you a lot, $(document).ready(function(){//your code with timer}) code with timer will need to reset the CSS property to normal after desired time(let's say 5 sec.)

For the demo( that you have described), this is initiated with the onhover event, please check this fiddle I have created for that.

That reproduces the results in a desktop browser, and will also work in iPad, just add your timer code to suit your requirements.



回答2:

Regarding the original question: the best solution to have scrollbars would be to use an external library (already recommended iScroll is good, but even jQuery UI itself contains scrollbars). But displaying ever-present scrollbars might deviate from the general iOS UI (see below).

Alternative would be to indicate with other GUI elements that the content is scrollable. Consider small gradient fields in the end of the element (the content fades to background there) that suggest that content continues when touched and scrolled.

In iOS5 overflow: scroll functions as expected, i.e it allows the the div to be scrolled up/down with one finger within the area specified by the dimensions of the div. But scrollable div doesn't have scrollbars. This is a bit different from the general UI in iOS(5). Generally there are no scrollbars also, but they appear when user starts scrolling a content area and fade out again after the touch event has stopped.



回答3:

To answer Sam Hasler comment above. Nicescroll 3 is a jquery plugin that does just what you want with fade in/out effect and work in all major Mobile/Tablet/Desktop browsers.

Live demo

Code:

$(document).ready(function() {    
    $("html").niceScroll({styler:"fb",cursorcolor:"#000"});
    $("#divexample1").niceScroll();//or styles/options below
    $("#divexample2").niceScroll("#wrapperexample2",{cursorcolor:"#0F0",boxzoom:true});
    $("#divexample3").niceScroll("#divexample3 iframe",{boxzoom:true});
  });


回答4:

If you want to have the scroll to be always visible,

Do not set -webkit-overflow-scrolling: touch

then set custom style for scrollbar

::-webkit-scrollbar {
    -webkit-appearance: none;// you need to tweak this to make it available..
    width: 8px;
}

You loss the momentum effect, but scrollbar will always be there.

(tested under iPhone 4/ iOS 7)



回答5:

Mobile safari, as far as I have seen won't support scrollbars. The best plugin I could find to get the job done is this.

Its Demos are available here. It also has multiple predefined skins to suit your application.

here's a sample of what you'll get -



回答6:

By convention, scrollbars are not used on iOS.

For a div with overflow: scroll, the only native way to scroll is with two fingers.

You might take a look at iScroll, a JavaScript library which handles touch events and implements single-finger momentum scrolling (what users generally expect in native apps) for divs.



回答7:

until ios5 you could not scroll internal divs - so you probably are not seeing a scroll bar when you try to scroll because there isn't one.

I haven't tested on ios5 but supposedly scrolling internal divs now works.

If it isn't an internal div then you should be able to see the scroll bar when it is scrolling only - this isn't just on ios anymore - lion has gotten rid of all native scroll bars too. You can only see them when a window is scrolling or when the window is first loaded.