【发布时间】:2021-07-23 22:26:11
【问题描述】:
我正在使用 Pure Vanilla JavaScript 创建 HTML 表单,其效果与在 SurveyMonkey 表单上的效果相同,例如 https://www.surveymonkey.com/r/Online-Product-Feedback-Survey-Template-new 上的示例
我想要实现的是让我的表单问题的不透明度变浅,并使一个屏幕上的问题不透明度:0;以便在向上和向下滚动时从用户那里获得焦点并使该表单的所有其他问题变暗与上例相同,从而完全可见。
为此,我在从不同资源获得不同提示后尝试了我的代码,并在此分享。查看我的工作的实时工作 JSFIDDLE 并在那里进行编辑。
HTML
<form>
<div class="questionDIV overlay">
<h3>Question # 1) Our purpose / vision / mission / values reflect delivering value for all stakeholders...</h3>
<p><label class="container">Yes<input type="radio" name="1" value="Yes"><span class="radiomark"></span></label></p>
<p><label class="container">No<input type="radio" name="1" value="No"><span class="radiomark"></span></label></p>
<p><label class="container">Partially<input type="radio" name="1" value="Partially"><span class="radiomark"></span></label></p>
</div>
<div class="questionDIV overlay">
<h3>Question # 1) Our purpose / vision / mission / values reflect delivering value for all stakeholders...</h3>
<p><label class="container">Yes<input type="radio" name="1" value="Yes"><span class="radiomark"></span></label></p>
<p><label class="container">No<input type="radio" name="1" value="No"><span class="radiomark"></span></label></p>
<p><label class="container">Partially<input type="radio" name="1" value="Partially"><span class="radiomark"></span></label></p>
</div>
<div class="questionDIV overlay">
<h3>Question # 1) Our purpose / vision / mission / values reflect delivering value for all stakeholders...</h3>
<p><label class="container">Yes<input type="radio" name="1" value="Yes"><span class="radiomark"></span></label></p>
<p><label class="container">No<input type="radio" name="1" value="No"><span class="radiomark"></span></label></p>
<p><label class="container">Partially<input type="radio" name="1" value="Partially"><span class="radiomark"></span></label></p>
</div>
<div class="questionDIV overlay">
<h3>Question # 1) Our purpose / vision / mission / values reflect delivering value for all stakeholders...</h3>
<p><label class="container">Yes<input type="checkbox" name="1" value="Yes"><span class="checkmark"></span></label></p>
<p><label class="container">No<input type="checkbox" name="1" value="No"><span class="checkmark"></span></label></p>
<p><label class="container">Partially<input type="checkbox" name="1" value="Partially"><span class="checkmark"></span></label></p>
</div>
<div class="questionDIV overlay">
<h3>Question # 1) Our purpose / vision / mission / values reflect delivering value for all stakeholders...</h3>
<p><label class="container">Yes<input type="checkbox" name="1" value="Yes"><span class="checkmark"></span></label></p>
<p><label class="container">No<input type="checkbox" name="1" value="No"><span class="checkmark"></span></label></p>
<p><label class="container">Partially<input type="checkbox" name="1" value="Partially"><span class="checkmark"></span></label></p>
</div>
<div class="questionDIV overlay">
<h3>Question # 1) Our purpose / vision / mission / values reflect delivering value for all stakeholders...</h3>
<p><label class="container">Yes<input type="checkbox" name="1" value="Yes"><span class="checkmark"></span></label></p>
<p><label class="container">No<input type="checkbox" name="1" value="No"><span class="checkmark"></span></label></p>
<p><label class="container">Partially<input type="checkbox" name="1" value="Partially"><span class="checkmark"></span></label></p>
</div>
</form>
CSS
.overlay {opacity:0.2}
JavaScript
//Example from https://www.javascripttutorial.net/sample/dom/event/visible-viewport/index.html
function isInViewport(el) {
const rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
const box = document.querySelector('.questionDIV');
document.addEventListener('scroll', function () {
if (isInViewport(box) == true) {
document.querySelector('.questionDIV').classList.remove("overlay");
} else {
document.querySelector('.questionDIV').classList.add("overlay");
}
}, {
passive: true
});
【问题讨论】:
标签: javascript html jquery css scroll