当前位置:网站首页>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();
}
边栏推荐
- 比尔·盖茨晒48年前简历:“没你们的好看”
- CTFshow,信息搜集:web1
- Novel Slot Detection: A Benchmark for Discovering Unknown Slot Types in the Dialogue System
- Niuke real problem programming - day15
- CTFshow,信息搜集:web5
- 一文读懂数仓中的pg_stat
- 拜拜了,大厂!今天我就要去厂里
- Discussion on CPU and chiplet Technology
- Andriod --- JetPack :LiveData setValue 和 postValue 的区别
- Lidar knowledge drops
猜你喜欢
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
Niuke real problem programming - Day9
Why do we use UTF-8 encoding?
Pandora IOT development board learning (HAL Library) - Experiment 12 RTC real-time clock experiment (learning notes)
CTFshow,信息搜集:web8
CTFshow,信息搜集:web6
Navigation - are you sure you want to take a look at such an easy-to-use navigation framework?
⼀个对象从加载到JVM,再到被GC清除,都经历了什么过程?
What is data leakage
Huawei cloud database DDS products are deeply enabled
随机推荐
A laravel background management expansion package you can't miss - Voyager
[understanding of opportunity -40]: direction, rules, choice, effort, fairness, cognition, ability, action, read the five layers of perception of 3GPP 6G white paper
数学建模——什么是数学建模
【服务器数据恢复】某品牌StorageWorks服务器raid数据恢复案例
时空可变形卷积用于压缩视频质量增强(STDF)
Compile advanced notes
Yyds dry goods inventory # solve the real problem of famous enterprises: cross line
一个需求温习到的所有知识,h5的表单被键盘遮挡,事件代理,事件委托
@ComponentScan
连接ftp服务器教程
8大模块、40个思维模型,打破思维桎梏,满足你工作不同阶段、场景的思维需求,赶紧收藏慢慢学
Emqx 5.0 release: open source Internet of things message server with single cluster supporting 100million mqtt connections
Pinduoduo lost the lawsuit, and the case of bargain price difference of 0.9% was sentenced; Wechat internal test, the same mobile phone number can register two account functions; 2022 fields Awards an
6. Electron borderless window and transparent window lock mode setting window icon
[server data recovery] a case of RAID data recovery of a brand StorageWorks server
What is the process of ⼀ objects from loading into JVM to being cleared by GC?
Wechat applet - Advanced chapter component packaging - Implementation of icon component (I)
Ctfshow, information collection: web7
“百度杯”CTF比赛 2017 二月场,Web:include
Niuke real problem programming - Day9