当前位置:网站首页>有关iframe锚点,锚点出现上下偏移,锚点出现页面显示问题.iframe的srcdoc问题
有关iframe锚点,锚点出现上下偏移,锚点出现页面显示问题.iframe的srcdoc问题
2022-06-24 06:55:00 【拿鼓思谱】
最近开发项目,遇到需要开发iframe页面,并且还要在外面弄个导航栏,点击后iframe要跳到对应的锚点位置.
遇到过一些坑,希望分享后能帮到大家.
1.后端是直接给的一个html文件,所以我创建时不是用iframe的src来接受,而是用srcdoc直接展示.大家可以去搜一下iframe.src是对应的url.而srcdoc是对应的html文件
2.锚点,我们是不能直接给iframe内部直接锚点的,因为iframe是不能直接操作.需要利用iframe的postMessage事件.具体的大家可以百度具体文档.方法
(1)在外面导航页面添加点击事件(比如说hanldClick)
hanldClick() {
// val是你点击的iframe对应的锚点id
document.getElementById('iframeId').contentWindow.postMessage(val, '*')
}
(2) 在iframe里面的script中,添加监听事件
window.addEventListener('message', function (e) {
console.log(e.data)
}, false)
在这里,就会遇到 坑:
坑一,锚点后会带动页面整体上移.千万不要用margin-top,用了后你都发现没啥鸟用.要用padding-top .其他小偏移自己处理了,这里不做详解
坑二,如果你在iframe里面像下面这么写,就会发现iframe变成了你现在的整个大的html页面,而且导航没啥用
window.addEventListener('message', function (e) {
const link = document.createElement('a');
link.href = `#${
e.data}`;
document.getElementById('#${e.data}').appendChild(link);
link.click();
}, false)
正确的写法是:
正确的写法是:
window.addEventListener('message', function (e) {
// 用window,不要用document,应该是作用域问题,有污染导致
window.location.hash = `#${
e.data}`;
}, false)
如果文章对您有所帮助,就在文章的右上角或者文章的末尾点个赞吧!(づ ̄ 3 ̄)づ
如果喜欢比卡丘分享的文章,就给比卡丘点个关注吧!(๑′ᴗ‵๑)づ╭~
鉴于个人经验有限,所有观点及技术研点,如有异议,请直接回复讨论(请勿发表攻击言论)
拿走,不用谢!!!送人玫瑰,手留余香
边栏推荐
- 3-列表简介
- Écouter le réseau d'extension SWIFT (source)
- JS implementation to check whether an array object contains values from another array object
- 解决 These dependencies were not found: * core-js/modules/es6.array.fill in xxx 之类的问题
- 不止于观测|阿里云可观测套件正式发布
- 5-if语句(选择结构)
- GraphMAE----論文快速閱讀
- Tuple remarks
- Simple refraction effect
- Practice of opengauss database on CentOS, configuration
猜你喜欢

Shader common functions

Latest news of awtk: new usage of grid control

Introduction to software engineering - Chapter 2 - feasibility study

Echart's experience (I): about y axis yaxis attribute

首次曝光 唯一全域最高等级背后的阿里云云原生安全全景图

Coordinate transformation of graphic technology

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

OpenGauss数据库在 CentOS 上的实践,配置篇

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

Chapitre 2: dessiner une fenêtre
随机推荐
Cold thinking on the hot track: multiplier effect is the fundamental requirement of East West calculation
解决笔记本键盘禁用失败问题
JDBC 在性能测试中的应用
[C language] system date & time
Open cooperation and win-win future | Fuxin Kunpeng joins Jinlan organization
Echart 心得 (一): 有关Y轴yAxis属性
Vulnhub target: boredhackerblog_ CLOUD AV
The first exposure of Alibaba cloud's native security panorama behind the only highest level in the whole domain
研究生英语期末考试复习
Review of postgraduate English final exam
Leetcode 174 Dungeon games (June 23, 2022)
慕思股份在深交所上市:毛利率持续下滑,2022年一季度营销失利
解决错误: LNK2019 无法解析的外部符号
Introduction to software engineering - Chapter 3 - Requirements Analysis
Any remarks
Part 1: building OpenGL environment
L1-019 who goes first (15 points)
[test development] first knowledge of software testing
【资料上新】迅为基于3568开发板的NPU开发资料全面升级
5g industrial router Gigabit high speed low delay