当前位置:网站首页>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 
边栏推荐
- 研究生英语期末考试复习
- Do you still have the opportunity to become a machine learning engineer without professional background?
- Keep one decimal place and two decimal places
- Standing at the center of the storm: how to change the engine of Tencent
- 有关iframe锚点,锚点出现上下偏移,锚点出现页面显示问题.iframe的srcdoc问题
- 一文理解同步FIFO
- redolog和binlog
- On the H5 page, the Apple phone blocks the content when using fixed to locate the bottom of the tabbar
- [ACNOI2022]做过也不会
- The first exposure of Alibaba cloud's native security panorama behind the only highest level in the whole domain
猜你喜欢

Moonwell Artemis is now online moonbeam network
![[run the script framework in Django and store the data in the database]](/img/6b/052679e5468e5a90be5c4339183f43.png)
[run the script framework in Django and store the data in the database]

Keep one decimal place and two decimal places

ImportError: cannot import name ‘process_ pdf‘ from ‘pdfminer. Pdfinterp 'error completely resolved

Case examples of corpus data processing (cases related to sentence retrieval)

Vulnhub靶机:BOREDHACKERBLOG_ CLOUD AV

C语言_字符串与指针的爱恨情仇

Shader common functions

单片机STM32F103RB,BLDC直流电机控制器设计,原理图、源码和电路方案

Swift Extension NetworkUtil(网络监听)(源码)
随机推荐
These dependencies were not found: * core JS / modules / es6 array. Fill in XXX
模型效果优化,试一下多种交叉验证的方法(系统实操)
Graphmae ---- quick reading of papers
自动化测试的未来趋势
Resolution error: LNK2019 unresolved external symbol
C语言_字符串与指针的爱恨情仇
4-操作列表(循环结构)
搜索与推荐那些事儿
You get in Anaconda
疫情下更合适的开发模式
Shader common functions
Application of JDBC in performance test
Vulnhub target: boredhackerblog: social network
Auto usage example
Getting started with crawler to giving up 06: crawler play Fund (with code)
Chapter 3: drawing triangles
从 jsonpath 和 xpath 到 SPL
In the post epidemic era, the home service robot industry has just set sail
decltype用法介绍
Oracle advanced SQL qualified query