- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我试图让 div .progress
容器在 div #myContact
滚动到 100% 高度时改变它的宽度
我试过将 HTML 元素与 onscroll、onchange 或其他任何东西链接起来,但我似乎不知道什么可行。
<div onscroll="scrollProgress()" id="containter" class="snap">
<div class="progress-container"> //this containter needs to change width
<div class="progress-bar" id="myContact"></div> //when this one reaches 100% height
</div>
</div>
<script src="scroll.js"> </script>
#containter {
width: 100%;
height: 3000px; // original setting is 100vh, this value is just for scrolling without me pasting all the other elemnts
scroll-behavior: smooth;
overflow: scroll;
scroll-snap-type: y mandatory;
}
.progress-container {
position: fixed;
width: 20%;
height: 100%;
}
/* The progress bar (scroll indicator) */
.progress-bar {
width: auto;
background: #000;
height: 0%;
max-height: 100%
}
// Scroll function
function scrollProgress() {
var elem = document.getElementById('containter');
var winScroll = elem.scrollTop;
var height = elem.scrollHeight - document.documentElement.clientHeight;
var scrolled = (winScroll / height) * 100;
document.getElementById("myContact").style.height = scrolled + "%";
};
// Expand function
/* When 'myContact' reaches 100% height then 'progres-container' expands left, and cover the entire screen*/
function expandMe() {
let trigger = document.getElementById('myContact').style.height;
if (trigger = 100) {
document.getElementById('progress').style.width = trigger + "%";
}
};
/* When 'myContact' reaches 100% height then 'progres-container' expands left,
and cover the entire screen*/
什么都没发生,我也没有错误。
非常感谢!
最佳答案
这不是像素完美的解决方案,但由于您的代码中有一些错误,事件监听器应该绑定(bind)到 window
对象,然后它会根据您的需要呈现。我为宽度变化样式添加了一些视觉效果。
window.addEventListener('scroll', function () {
var winScroll = window.pageYOffset;
var height = document.getElementById('container').scrollHeight - document.documentElement.clientHeight;
var scrolled = (winScroll / height) * 100;
document.getElementById("myContact").style.height = scrolled + "%";
document.getElementById("myContact").parentElement.style.width =(scrolled >= 100) ? scrolled + "%" : "20%";
});
#container {
width: 100%;
height: 3000px;
scroll-behavior: smooth;
overflow: scroll;
scroll-snap-type: y mandatory;
}
.progress-container {
position: fixed;
background: #0f0;
width: 20%;
height: 100%;
transition: width 1s;
}
/* The progress bar (scroll indicator) */
.progress-bar {
width: auto;
background: #f00;
height: 0%;
max-height: 100%
}
<div id="container" class="snap">
<div class="progress-container">
<div class="progress-bar" id="myContact"></div>
</div>
</div>
关于javascript - 将 HTML 元素与 onscroll 链接,onchange 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56198697/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!