当前位置:网站首页>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,信息搜集:web9
- CTFshow,信息搜集:web14
- 2022年5月互联网医疗领域月度观察
- Ctfshow, information collection: web1
- Niuke real problem programming - day16
- The method of parsing PHP to jump out of the loop and the difference between continue, break and exit
- @ComponentScan
- [Yugong series] go teaching course 005 variables in July 2022
- ⼀个对象从加载到JVM,再到被GC清除,都经历了什么过程?
- How bad can a programmer be? Nima, they are all talents
猜你喜欢
Ctfshow, information collection: web7
Cocoscreator operates spine for animation fusion
《微信小程序-进阶篇》组件封装-Icon组件的实现(一)
Ctfshow, information collection: web6
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
[today in history] July 7: release of C; Chrome OS came out; "Legend of swordsman" issued
拜拜了,大厂!今天我就要去厂里
什么是云原生?这回终于能搞明白了!
Summer safety is very important! Emergency safety education enters kindergarten
#yyds干货盘点# 解决名企真题:交叉线
随机推荐
Navigation — 这么好用的导航框架你确定不来看看?
Jetson AGX Orin CANFD 使用
Niuke real problem programming - day18
Lidar knowledge drops
Niuke real problem programming - Day10
Electronic remote error
用于增强压缩视频质量的可变形卷积密集网络
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
众昂矿业:萤石继续引领新能源市场增长
Apache多个组件漏洞公开(CVE-2022-32533/CVE-2022-33980/CVE-2021-37839)
防火墙基础之服务器区的防护策略
CTFshow,信息搜集:web7
什麼是數據泄露
缓冲区溢出保护
比尔·盖茨晒48年前简历:“没你们的好看”
最安全的证券交易app都有哪些
Infinite innovation in cloud "vision" | the 2022 Alibaba cloud live summit was officially launched
Es log error appreciation -maximum shards open
广州开发区让地理标志产品助力乡村振兴
⼀个对象从加载到JVM,再到被GC清除,都经历了什么过程?