javascript - 重定向到某些 div 中的 url
问题描述
是否可以重定向到某些 div 中的 URL
喜欢
<div id="id45">
<! -- redirect to url if reached to this div content -->
</div>
解决方案
例如,您可以这样做:
function myFunction() {
if (window.innerHeight + window.scrollY >= document.getElementById("id45").offsetTop) {
window.open("https://www.example.com");
window.removeEventListener("scroll", myFunction);
}
}
window.addEventListener("scroll", myFunction);
<div style="width: 100%; height: 110vw; background-color: lightBlue;"></div>
<div id="id45" style="width: 100%; height: 100px; background-color: red;">
<! -- redirect to url if reached to this div content -->
</div>
对我来说,它有效,但不在这个 Stackoverflow 的片段中。它获得了完整浏览器窗口的高度,但是如果您将其复制到您的网站,我希望它会起作用。
注意:我的浏览器阻止了此窗口的打开,但这是正常的,如此处所述。
推荐阅读
- spring-boot - 如何修复 spring-boot 保险库集成中的“由于代理问题导致保险库连接失败”?
- c# - 通过 MSBuildLocator 加载时,MSBuild 16.0 找不到自己的依赖程序集
- javascript - ROR:点赞按钮,无刷新
- mysql - MySQL 合并两个可为空的 BIT 给出结果 = 48
- arrays - Bash 使用正则表达式扩展文件名
- spring - QueryDSL 构建错误 AnnotationProcessor ''未找到 Spring Boot
- bash - 有没有办法接受一个行为类似于 bash 中的文件的输入?
- jenkins - 带有选择参数的詹金斯触发器
- generator - GAN,由真实图像生成回归输出,而不是随机噪声
- javascript - 带有 iOS 14 的 VideoJs