当前位置:网站首页>A need to review all the knowledge, H5 form is blocked by the keyboard, event agent, event delegation
A need to review all the knowledge, H5 form is blocked by the keyboard, event agent, event delegation
2022-07-07 15:01:00 【imkaifan】
Two involved dom The method on the :
Element.scrollIntoViewIfNeeded()
Element.scrollIntoViewIfNeeded() Method is used to scroll elements that are not in the visible area of the browser window to the visible area of the browser window .
If the element is already visible in the browser window , No scrolling .
grammar :
element.scrollIntoViewIfNeeded(); // Equate to element.scrollIntoViewIfNeeded(true)
element.scrollIntoViewIfNeeded(true);
element.scrollIntoViewIfNeeded(false);
Parameters :
One Boolean Type value , The default is true:
If true, Then the element will be centered in the visible area of its scrolling area .
If false, Then the element will be aligned with the closest edge of the viewable area of its scrolling area . According to which edge of the element is closest to the visible area ,
The top of the element will be aligned with the top edge of the visible area , Or the bottom edge of the element will be aligned with the bottom edge of the visible area .
Be careful : Nonstandard : This feature is nonstandard , Please try not to use it in a production environment !(2022.07.06)
Element.scrollIntoView()
Element Interface scrollIntoView() Method scrolls the parent container of the element , Cause to be called scrollIntoView() The element is visible to the user .
grammar :
element.scrollIntoView(); // Equate to element.scrollIntoView(true)
element.scrollIntoView(alignToTop); // Boolean Type parameter
element.scrollIntoView(scrollIntoViewOptions); // Object Type parameter
Parameters :
One Boolean value :
1、 If true, The top of the element will be aligned with the top of the visible area of the scrolling area it is in .
Corresponding scrollIntoViewOptions: {block: "start", inline: "nearest"}. This is the default value for this parameter .
2、 If false, The bottom of the element will be aligned with the bottom of the visible area of the scrolling area it is in .
Corresponding scrollIntoViewOptions: {block: "end", inline: "nearest"}.
An object that contains the following properties :
behavior Optional
Define animation transitions , "auto" or "smooth" One of . The default is "auto".
block Optional
Define the vertical alignment , "start", "center", "end", or "nearest" One of . The default is "start".
inline Optional
Define the horizontal alignment , "start", "center", "end", or "nearest" One of . The default is "nearest".
var element = document.getElementById("box");
element.scrollIntoView();
element.scrollIntoView(false);
element.scrollIntoView({
block: "end"});
element.scrollIntoView({
behavior: "smooth", block: "end", inline: "nearest"});
Be careful : It depends on the layout of other elements , This element may not scroll completely to the top or bottom .
The actual combat :
// my html structure
<div class="container" ref="containerForm" v-huiLoading="loading">
<hm-scroll>
<hm-validator-group :result="validate">
<div class="form-item">
<div class="label-input">
<div class="name name-require">{
{
$t('vamc_mobile_handle_form_customer_name') }}</div>
<hm-input :placeholder="$t('vamc_mobile_input_tip')" v-model="userForm.Name"></hm-input>
</div>
<hm-validator :model="userForm.Name" :rules="rules.Name" :messages="messages.Name"></hm-validator>
</div>
<div class="form-item" style="margin-top: 8.15px">
<div class="label-input">
<div class="name name-require">{
{
$t('vamc_mobile_handle_form_street_address') }}</div>
<hm-input :placeholder="$t('vamc_mobile_input_tip')" v-model="userForm.Street" @blur="getAlternativeAddress"></hm-input>
</div>
<hm-validator :model="userForm.Street" :rules="rules.Street" :messages="messages.Street"></hm-validator>
</div>
<div class="form-item" style="margin-top: 4px">
<div class="label-input">
<div class="name name-require">{
{
$t('vamc_mobile_handle_form_city_town') }}</div>
<hm-input :placeholder="$t('vamc_mobile_input_tip')" v-model="userForm.City"></hm-input>
</div>
<hm-validator :model="userForm.City" :rules="rules.City" :messages="messages.City"></hm-validator>
</div>
</hm-scroll>
</div>
</template>
How to find it online 1:
Add two knowledge points :
1、resize Method no matter the width change or height change of the screen can be detected , Can trigger this method .
2、Document Interface activeElement Read-only property , Used to return the current in DOM perhaps shadow DOM Focused in the tree Element.
if ((/Android/gi).test(navigator.userAgent)) {
window.addEventListener('resize', function () {
if (document.activeElement.tagName == 'INPUT' ||
document.activeElement.tagName == 'TEXTAREA') {
window.setTimeout(function () {
document.activeElement.scrollIntoViewIfNeeded();
}, 0);
}
});
}
How to find it online 2( It's quite correct to say in detail , Specific links ):
const ua = navigator.userAgent;
const iOS = /iPad|iPhone|iPod/.test(ua);
const input = document.querySelector('#input');
input.addEventListener('focus', () => {
setTimeout(() => {
if (iOS) {
if (!/OS 11_[0-3]\D/.test(ua)) {
document.body.scrollTop = document.body.scrollHeight;
}
} else {
input.scrollIntoView(false);
}
}, 300);
});
I'll take care of it :
i: The event is entrusted to solve
ii: Learn from the methods found on the Internet 2 To transform
First delegate from the event ( Event agent ) Talking about!
- js In the event of bubble, we know , Events on child elements will bubble up on parent elements .
- Event agent is , Events originally added to child elements , On his father .
- Then there is a problem : The parent has so many child elements , How to distinguish which child element an event should be ? The answer is :event There are... Recorded in the object “ Event source ”, It is the child element of the event . It has compatibility problems , In the old IE Next , The source of the event is window.event.srcElement, Other browsers are event.target
What are the benefits of using event delegation ? Concrete demo Please refer to the example, which is well written , It simply and clearly expounds the above 123
The first advantage is high efficiency , such as , no need for The loop adds events to the child elements
The second advantage is ,js There is no need to add events to the newly generated child elements , The program logic is more convenient
Specific function implementation code :
// methods: Can only act for click The incident is over , because onfocus Agent cannot be found
handleKeyboardCover() {
const ua = navigator.userAgent;
const iOS = /iPad|iPhone|iPod/.test(ua);
this.$refs['containerForm'].onclick = function(event) {
let dom = event.srcElement || event.target;
if (dom.nodeName.toLowerCase() === 'input' || dom.nodeName.toLowerCase() === 'textarea') {
console.log(' Set out to get click events ', event, event.target);
setTimeout(() => {
if (iOS) {
if (!/OS 11_[0-3]\D/.test(ua)) {
// document.body.scrollTop = document.body.scrollHeight; // When ios Handle when the keyboard is blocked on
}
} else {
event.target.scrollIntoView({
behavior: "smooth", block: "center", inline: "nearest"});
}
}, 300);
}
};
}
// Life cycle
mounted() {
this.handleCreateMap();
this.handleKeyboardCover();
}
边栏推荐
- 拜拜了,大厂!今天我就要去厂里
- CTFshow,信息搜集:web7
- 【OBS】RTMPSockBuf_Fill, remote host closed connection.
- Concurrency Control & NoSQL and new database
- 知否|两大风控最重要指标与客群好坏的关系分析
- Ctfshow, information collection: web2
- CTFshow,信息搜集:web8
- Shengteng experience officer Episode 5 notes I
- 15、文本编辑工具VIM使用
- Apache multiple component vulnerability disclosure (cve-2022-32533/cve-2022-33980/cve-2021-37839)
猜你喜欢
Ctfshow, information collection: web2
15、文本编辑工具VIM使用
Niuke real problem programming - day15
Ctfshow, information collection: Web3
Bill Gates posted his resume 48 years ago: "it's not as good-looking as yours."
拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条...
How to enable radius two factor / two factor (2fa) identity authentication for Anheng fortress machine
CTFshow,信息搜集:web9
Stm32cubemx, 68 sets of components, following 10 open source protocols
CTFshow,信息搜集:web14
随机推荐
Niuke real problem programming - day14
Huawei cloud database DDS products are deeply enabled
Notes HCIA
Delete a whole page in word
CPU与chiplet技术杂谈
【服务器数据恢复】戴尔某型号服务器raid故障的数据恢复案例
CTFshow,信息搜集:web14
How to enable radius two factor / two factor (2fa) identity authentication for Anheng fortress machine
一文读懂数仓中的pg_stat
13 ux/ui/ue best creative inspiration websites in 2022
Five pain points for big companies to open source
How bad can a programmer be? Nima, they are all talents
比尔·盖茨晒48年前简历:“没你们的好看”
MySQL installation configuration 2021 in Windows Environment
Pandora IOT development board learning (HAL Library) - Experiment 12 RTC real-time clock experiment (learning notes)
广州开发区让地理标志产品助力乡村振兴
Ctfshow, information collection: web6
Integer learning
Apache multiple component vulnerability disclosure (cve-2022-32533/cve-2022-33980/cve-2021-37839)
With 8 modules and 40 thinking models, you can break the shackles of thinking and meet the thinking needs of different stages and scenes of your work. Collect it quickly and learn it slowly