Now the text-reading app on the phone is very rich, and the good reading experience and the huge stacks often make me feel very excited.
I think about 8 years ago with the almost-inch screen of the MP3 read the story of e-book, a faint warmth. A little longer, small, I also often and small groups of friends to the bookstore to see petition, that is, white reading. The pile of heavy yellow novels on the ancient wooden shelves seemed to us to be more delicious than the lunch meat on the plate in our young eyes.
In today's information-based era, reading has become unprecedented convenience, but the feeling of anticipation and excitement in childhood disappeared in the long river of time.
The years are passing, the times are progressing.
Willing to put down all the impetuous, happy in the new era of life, no matter how tomorrow, I believe it must be better than today.
This article takes a Web-based reader as a case, showing some of the processing of scrollbars in JavaScript, which is what it will look like after completion:
When I scroll up the scroll bar, an upward arrow appears in the lower right corner of the screen:
and scroll down, and then automatically disappear.
When I click on this translucent arrow button, it will automatically slide to the top of the section.
This chapter implements this small function.
Development tools: Hbuilder (Personal love, submissive trend gave up the use of the big six months of EditPlus, but EditPlus did exercise my ability to spell words)
Test environment: Google Chrome
Body
1. Page layout and drawing
We write a basic HTML template
<! DOCTYPE html>
Next, we bring in the background image (the size is slightly adjusted):
body{ Background:url (bg.jpg) no-repeat; background-size:100%;}
Let's start by writing a Div as the parent container for the entire phone.
In its CSS style, we did a center-positioning (horizontal).
. phone { width:322px; height:550px; position:relative; left:35%; top:35px; Background: #66CC00;}
<div class= ' phone ' ></div>
Next, introduce the style picture at the top and bottom.
In fact, the top and bottom of the phone are two pictures:
We first introduced the top picture, before the introduction of the picture, the first to draw two div to put the picture.
<body> <div class= ' phone ' > <div class= ' phone_top ' ></div> <div class= ' Phone-bottom ' ></div> </div></body>
Then, put the picture in the way of the background map.
. phone. phone_top { background:url (phone_top.png); height:42px;}. Phone. Phone-bottom { background:url (phone_bottom.png); Position:absolute; height:42px; width:100%; bottom:0;}
In this way, the approximate mold of a cell phone comes out, then we add the screen area.
. Phone. container{ overflow-x: hidden; Overflow-y: auto; width:90%; Background: #ccc; height:456px; font-size:14px; Text-align:left; Background: #dcf3dc; font-family: Microsoft ya black; Color: #555; line-height:28px; padding:16px; Text-indent:2em; padding:16px 16px 0px 16px;}
<body> <div class= ' phone ' > <div class= ' phone_top ' ></div> <div class= ' Container ' ></div> <div class= ' phone-bottom ' ></div> </div></body>
OK, now you can remove the background color of the parent container.
Background: #66CC00; Remove
In order to make the phone model more like a bit, we manually add a button, the amount of personal overdraft animation one bar.
. back { width:30px; height:30px; Position:absolute; left:50%; Margin-left: -15px; border:2px solid #c7bcbc; top:4px; border-radius:50%;}
<div class= ' Phone-bottom ' > <span class= ' Back ' ></span></div>
Although span is an inline element, because we set it to Position:absolute, the width and height still work.
I was also writing this case inadvertently discovered, I have always thought that it is necessary to manually upgrade the inline elements to block-level elements.
In this way, our page layout is almost finished.
2. Text part design and beautification
Next, we simulate some data for the reader.
<body> <div class= ' phone ' > <div class= ' phone_top ' ></div> <div class= ' container ' >
The scroll bar style is not very beautiful, we will beautify it
/** scroll bar Styling */
::-webkit-scrollbar{width:5px;height:6px;background: #ccc;}::-webkit-scrollbar-button{background-color: #e5e5e5;} ::-webkit-scrollbar-track{background: #999;}::-webkit-scrollbar-track-piece{background: #ccc}::- Webkit-scrollbar-thumb{background: #666;}::-webkit-scrollbar-corner{background: #82AFFF;}::- Webkit-scrollbar-resizer{background: #FF0BEE;} Scrollbar{-moz-appearance:none!important;background:rgb (0,255,0)!important;} Scrollbarbutton{-moz-appearance:none!important;background-color:rgb (0,0,255)!important;} Scrollbarbutton:hover{-moz-appearance:none!important;background-color:rgb (255,0,0)!important;}
It's much nicer.
The headline part is a bit abrupt, we give four beautification suggestions:
1. Title Left Justified
2. Draw a line at the bottom, separate from the body of the novel, and open some space.
3. The font color is slightly lighter, not too dark
4. Slightly larger between the characters
So
. Phone. Container h4 { text-indent:0; Margin-bottom:1em; Color: #736357; border-bottom:1px solid #736357; letter-spacing:2px;}
This looks good, of course, everyone's aesthetic is different, you can also tune into your favorite style.
Between the paragraphs and the text spacing is too small, we also adjust, not so stingy, haha.
. Phone. container p { margin-bottom:15px; letter-spacing:2px;}
Well, that's better.
3. Bring up the arrow icon
We have prepared a translucent arrow icon, which is now introduced.
<body> <div class= ' phone ' > <div class= ' phone_top ' ></div> <div class= ' container ' > <span id= ' totop ' ></span>
The styles are as follows:
. Phone. Container #toTop { width:40px; height:60px; Display:inline-block; Position:absolute; Background:url (top.png) no-repeat; background-size:100%; bottom:80px; right:15px; opacity:0.7;}
The introduction came.
4. JS Control
We use the Animate method of jquery to get back to the top of the animation, the core logic to achieve this function is to control the height of the scroll bar distance from the top, that is, scrolltop, let it become 0.
Stand-alone icon returns directly to the top
$ (' #toTop '). On (' click ',function() { $ ('. Phone. Container '). EQ (0). Stop (true True). Animate ({scrolltop:0},500,function() { $ (' #toTop '). CSS ({' opacity ': 0}); }); return false ;});
Finally, one effect that we want to achieve is that the button is displayed only when the scrollbar is dragged upwards, or the button is hidden. After all, we don't want to have a small icon all the time while we're reading.
The realization of the idea is also very simple, is to determine whether the current scroll bar scrolling upward, or scroll down?
Then set the button transparency on the line, this time, we need to listen to the scroll bar, if the scroll up to show the button, otherwise hide the button, the implementation code is as follows:
var justscrolltop = 0; Record the position of the last scroll bar from the top
Scroll bar Listener Events
$ ('. Phone. Container '). On (' scroll ', function (e) { if (E.target.scrolltop > Justscrolltop) { $ (' #toTop '). CSS ({' Opacity ': 0}); Hide }else{ $ (' #toTop '). CSS ({' Opacity ': 0.8}); Show } justscrolltop = E.target.scrolltop;});
Effect:
Original link http://www.jianshu.com/p/0d12a945f0d6#
JavaScript implements mobile web-page Reader