Summary of mobile h5 Development (III)
I have previously written two issues and solutions encountered during development. At that time, CSS and JavaScript were written separately. When I write this article, I feel that a lot of content is internally related, so it is difficult to separate it.
Let's share your feelings over the past six months:
There is a great distance between understanding and understanding. When someone talks about a knowledge point, they can talk about it and express their opinions. Understanding what knowledge points can be used to solve a problem.
Therefore, I have seen many knowledge points in books, but I don't know how to use them or how to use them when I encounter problems, sometimes some colleagues give instructions on how to solve the problem. The key is to read more (read books, read other people's Code) and use more.
1. display: none; and visibility: hidden;
display:none
Disables the display of an element (which has no effect on the layout). All its descendant elements are also disabled. This element does not exist during document rendering. (The location is not displayed in the Document Stream, but the DOM node will still appear in the Document Stream)
visibility:hidden
The visibility attribute allows you to control the visibility of a graphic element, but it still occupies the position in the Document Stream during display.
Usedisplay:none
The element is not displayed, but the DOM node still appears, so we can use the selector to operate the element. Example in:
2. Problems Caused by event bubbling
This problem occurs in the previous article "h5 terminal calls the camera to scan the QR code and parse it. For detailed code, see the article.
Problem scenarios
First look at the html code:
Scan QR code 1
-
My previous thought was like this:
1. I first triggerqr-btn
Ofclick
Event, triggered in callbackinput
Ofclick
Eventclick
Event
2. then triggerinput
Ofchange
Event to obtain the information of the uploaded image.
According to my ideas, the Code should look like the following:
// Click the event of the parent Element
$('.qr-btn').bind('click',function(){
// Trigger the subelement event
$('[node-type=jsbridge]').trigger("click");
});
$('[node-type=jsbridge]').bind('change',function(){
// Do something
});
The above code, according to the normal idea, should be no problem, but there is a problem in the actual running process. The browser reports the following error information:
This is because of stack overflow. So why is this problem? I place the breakpoint at the following position and click the child element.
The following situations occur:Unlimited code triggering$('.qr-btn').bind(...)
The above error message is displayed. So why?
Think about it and find that:It is because of event bubbles. Clickclick
Event, sub-elementclick
Event bubbles to the parent element to triggerclick
Event, and then the parent element triggersclick
Event, which leads to a loop of events.
Solution:
How can I solve the problem by trying to prevent event bubbles?
Then we try to trigger the sub-elementclick
Try to bubble the sub-elements to see if they can solve my problem? Add the following code:
$('[node-type=jsbridge]').bind('click',function(e){
// console.log(e.type);
e.stopPropagation();
});
After my tests, the code can run normally.
Is there a better way to solve the above problem? Please refer to the following content
3. for attributes of lable labels
Let's take a looklable
Tag definition:
Label isinput
Element Definition annotation (TAG ).
label
The element does not present any special effect to the user. However, it improves availability for mouse users. If you click Text in the label element, this control is triggered. That is to say, when the user selects the tag, the browser automatically redirects the focus to the form control related to the tag.
Labelfor
The property must be the same as the id property of the relevant element.
Let's take a look at the sample code and effects of w3school:
The effect is as follows:
Here we should know how to improve our code,
Scan QR code 1
In addition to the style of the lable label, we need to define it ourselves. There are two advantages:
- Reduces JavaScript writing;
lable
Labels andinput
Labels do not need to be contained. 4. Comparison of advantages and disadvantages between the "bullet box" layout and the general box model layoutRecently, a lottery activity was held, with an animation of the rotation of the wheel (note that the choppy gif image in the middle is played again )., The effect is as follows:
We will continue to introduce animation implementation in the next article. Here we will focus on the layout issues. This is because we have different pages in pc and mobile. Therefore, I have used two solutions for pc and mobile respectively. The traditional pc layout is implemented, and the h5 "elastic box" is implemented.
1. Elastic box implementationThe lights on the periphery are made of absolute positioning, but there are not too many introductions. The main thing is to look at the nine cells in the middle of the prize. The html code is as follows:
Mac pro sweeping robot iphone6s20 credit coupon ps4Monkey year limit
Lucky lucky moneyThe css code is as follows:
.re-middle { position: absolute; width: 28.3rem; height: 16rem; top: 0; left: 0; right: 0; bottom: 0; margin: auto; background-color: #f69f75; color: #ffdece; font-size: 1.8rem;}.row-a,.row-b,.row-c { height: 5.3rem; display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-flex-flow: row nowrap; -ms-flex-flow: row nowrap; flex-flow: row nowrap;}.row-a div,.row-b div,.row-c div { -webkit-box-flex: 1; -webkit-flex: 1; -ms-flex: 1; flex: 1; text-align: center; line-height: 5.3rem; background-color: #f69f75;}
The above css Code shows that I only use the "elastic box" in the horizontal direction, while in the vertical direction, I still use a fixed height (because I use the rem unit, the height here is not accurate and will be calculated based on the fontsize value .)
Can I use an elastic box both vertically and horizontally?
Let's take a look at the following css code:
.re-middle {
position: absolute;
width: 28.3rem;
height: 16rem;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
background-color: #f69f75;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
color: #ffdece;
font-size: 1.8rem;
}
-
.row-a,
.row-b,
.row-c {
/*height: 5.3rem;*/
-webkit-box-flex: 1;
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-flow: row nowrap;
-ms-flex-flow: row nowrap;
flex-flow: row nowrap;
}
-
.row-a div,
.row-b div,
.row-c div {
-webkit-box-flex: 1;
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
text-align: center;
line-height: 5.3rem;
background-color: #f69f75;
/*position: relative;*/
-webkit-box-align:center;
-webkit-align-items:center;
-ms-flex-align:center;
align-items:center;
}
At the weekend, I read the "elastic box" document about the layout and finally realized the horizontal vertical center of the content in both vertical and vertical directions.In fact, the above Code only needs to define the content's parent elementdisplay:flex
Add two more attributesjustify-content
Andalign-items
You can. The former controls the vertical center of the content of the elastic box, and the latter controls the horizontal center of the content.
The Code is as follows:
.row-a div,
.row-b div,
.row-c div {
-webkit-box-flex: 1;
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
border: 1px solid #000;
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
2. Traditional implementationCompared with the h5 end, I implement the traditional pc end.FloatingMethod. My HTML code is as follows:
-
-
-
mac pro
Lucky Red Packets
iphone 6s
-
-
Coupon
-
20 points
-
-
Robot sweeping
Monkey year limit
Doll
-
ps4
-
-
-
The css code is as follows:
.re-middle {
background-color: #f89f71;
width: 530px;
height: 320px;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
-
.row-a,
.row-b,
.row-c {
/*height: 106px;*/
font-size: 0;
overflow: hidden;
}
-
.row-a > div,
.row-c > div {
float: left;
width: 176px;
height: 106px;
text-align: center;
}
-
.row-b div {
float: left;
width: 176px;
height: 106px;
text-align: center;
line-height: 106px;
background-color: #f69f75;
}
From the comparison of the above css code, we can clearly see the advantages and disadvantages of the traditional floating layout and the elastic box layout:
float
The layout code is concise, but the width and height of the specified box must be determined. The multi-screen adaptation will be slightly different (except for rem dynamic computing ).
- The "elastic box" layout Code uses the new css3 attribute. You need to add additional vendor prefixes to increase the complexity of the Code. (To add a vendor prefix, you can use the sublime plug-in to complete it with one click, recommended sublime configuration of front-end development engineers in my article)
- The elastic box provides convenience for multi-screen adaptation. I don't have to worry about the width and height of the sub-element, or the width of the screen.
flex
It will adapt itself.The following is a small problem:
The text element in the jiugongge, if it is only a single line, you only need to useline-height
The problem can be solved, but what if there are multiple rows? What will happen? See:
So we can only consider not to useline-height
, Usepadding
To solve the problem, trypadding
. For example:
We can see that there is an additional part below the container. That's what we use.padding
So how to solve this problem? This requires the use of the previously mentionedbox-sizing
To solve the problem.
.row-c-sec {
color: #ffdece;
font-size: 30px;
padding-top: 17px;
background-color: #f69f75;
/* Make the container height = content height + padding + border */
box-sizing: border-box;
}
5. Solution for multiple button submissionsThe plug-in for "Running Horse lights" encountered a problem, that is, when the user clicks the start lottery button and does not return the result, the user clicks the lucky draw button for the second time, at that time, there was a strange problem. For example, if the result returned when the first request and the second request overlap shows one, the interaction is unfriendly even if the user is allowed to perform a second lottery. In addition, if the front-end page is not limited, the display will also have a strange problem. For example:
Isn't that bad...
So how can I solve this problem?
The answer is simple. After I click the button, a transparent bullet layer pops up with absolute positioning and overwrites the button. After the result is returned and displayed, I remove the bullet layer at the same time. This avoids repeated submission by users. Take a closer look at the Code:
-
.cover-layer{
width:100%;
height:100%;
top:0;
position:absolute;
z-index:9999;
}
Here, make sure that my transparent bullet layer can be covered by the lottery button. Of course.class
YesJavaScript
Dynamic addition and deletion.
$(node).on('clcik','.reward-btn',function(){
// Call the pop-up layer
$('[node-type=cover_layer]',node).addClass('cover-layer');
.....
-
// Remove the bullet layer after the returned result
$('[node-type=cover_layer]',node).removeClass('cover-layer');
.....
});
This is the next time we will share the JavaScript development process of the "Carousel" lottery.
$ (Node ). on ('clc ','. reward-btn ', function () {// call the pop-up layer $ (' [node-type = cover_layer] ', node ). addClass ('Cover-lay ');..... // remove the bullet layer $ ('[node-type = cover_layer]', node) after the result is returned ). removeClass ('Cover-player ');.....})