当前位置:网站首页>About the iframe anchor, the anchor is offset up and down, and the anchor has page display problems Srcdoc problem of iframe
About the iframe anchor, the anchor is offset up and down, and the anchor has page display problems Srcdoc problem of iframe
2022-06-24 08:13:00 【Take drums and think about music】
Recent development projects , Need to develop iframe page , And get a navigation bar outside , After clicking iframe To jump to the corresponding anchor position .
Encountered some pits , I hope it can help you after sharing .
1. The back end is a direct one html file , So I didn't create it with iframe Of src To accept , It's about using srcdoc Show directly . You can search for iframe.src Is the corresponding url. and srcdoc Is the corresponding html file
2. Anchor point , We can't just give iframe Internal direct anchor , because iframe It can't be operated directly . Need to use iframe Of postMessage event . Specific you can baidu specific documents . Method
(1) Add a click event to the outside navigation page ( for instance hanldClick)
hanldClick() {
// val You clicked iframe The corresponding anchor point id
document.getElementById('iframeId').contentWindow.postMessage(val, '*')
}
(2) stay iframe Inside script in , Add listening Events
window.addEventListener('message', function (e) {
console.log(e.data)
}, false)
ad locum , Will meet pit :
A pit , The anchor point will drive the page to move up as a whole . Never use margin-top, After you use it, you find it useless . Use padding-top . Other small offsets are handled by ourselves , I don't want to elaborate here
Pit two , If you are in the iframe It reads like this , You will find iframe It has become your whole big html page , And navigation is useless
window.addEventListener('message', function (e) {
const link = document.createElement('a');
link.href = `#${
e.data}`;
document.getElementById('#${e.data}').appendChild(link);
link.click();
}, false)
The correct way to write it is :
The correct way to write it is :
window.addEventListener('message', function (e) {
// use window, Do not use document, It should be the scope problem , Caused by pollution
window.location.hash = `#${
e.data}`;
}, false)
If the article helps you , Just like it in the upper right corner of the article or at the end of the article !(づ ̄ 3 ̄)づ
If you like the articles shared by Pikachu , Just pay attention to bikachu !(๑′ᴗ‵๑)づ╭~
Given the limited personal experience , All viewpoints and technical research points , If there is any objection , Please reply directly to the discussion ( Do not make offensive remarks )
take , You're welcome !!! Send people roses , Fragrance in hand 
边栏推荐
- Unity culling related technologies
- 【点云数据集介绍】
- Leetcode 515 find the leetcode path of the maximum [bfs binary tree] heroding in each row
- 蓝桥杯_N 皇后问题
- 没有专业背景,还有机会成为机器学习工程师吗?
- Opening chapter of online document technology - rich text editor
- Decltype usage introduction
- C# Lambda
- Qopengl display point cloud file
- Swift 基础 闭包/Block的使用(源码)
猜你喜欢
![[C language] system date & time](/img/de/faf397732bfa4920a8ed68df5dbc48.png)
[C language] system date & time

More appropriate development mode under epidemic situation

OC Extension 检测手机是否安装某个App(源码)

Part 1: building OpenGL environment

SVN实测常用操作-记录操作大全

Keep one decimal place and two decimal places

Pipeline concept of graphic technology

Vulnhub target: boredhackerblog_ CLOUD AV

Resolution error: LNK2019 unresolved external symbol

Chapter 3: drawing triangles
随机推荐
JVM underlying principle analysis
More than observation | Alibaba cloud observable suite officially released
Chapter 3: drawing triangles
Graphmae ---- quick reading of papers
Search and recommend those things
Pagoda panel installation php7.2 installation phalcon3.3.2
软件工程导论——第二章——可行性研究
Chapitre 2: dessiner une fenêtre
In the post epidemic era, the home service robot industry has just set sail
快速读论文----AD-GCL:Adversarial Graph Augmentation to Improve Graph Contrastive Learning
Dart development server, do I have a fever?
Configure your own free Internet domain name with ngrok
AWTK 最新动态:Grid 控件新用法
Case examples of corpus data processing (cases related to sentence retrieval)
Application of JDBC in performance test
Svn actual measurement common operation record operation
C语言_字符串与指针的爱恨情仇
LINQ 查询(2)
[ACNOI2022]做过也不会
Live wire, neutral wire and ground wire. Do you know the function of these three wires?