有时,软件安装程序会强制您在启用“我同意”框之前滚动到EULA的末尾。如何在网页上产生相同的效果?
答案 0 :(得分:5)
<html>
<head>
<script type="text/javascript">
function setupPage() {
var agreement = document.getElementById("agreetext");
var visibleHeight = agreement.clientHeight;
var scrollableHeight = agreement.scrollHeight;
if (scrollableHeight > visibleHeight) {
var checkbox = document.getElementById("agreebox");
checkbox.checked=false;
checkbox.disabled=true;
agreement.onscroll = handleScroll;
}
}
function handleScroll() {
var agreement = document.getElementById("agreetext");
var visibleHeight = agreement.clientHeight;
var scrollableHeight = agreement.scrollHeight;
var position = agreement.scrollTop;
if (position + visibleHeight == scrollableHeight) {
document.getElementById("agreebox").disabled=false;
}
}
</script>
</head>
<body>
<form>
<textarea id="agreetext" rows="8" cols="40">Long agreement</textarea>
<br/><br/>
<input type="checkbox" id="agreebox" value="true"/> <label id="agreelabel" for="agreebox">I agree</label>
<br/><br/>
<input type="submit" value="Continue"/>
</form>
<script type="text/javascript">
// We put this at the end of the page rather than in an document.onload
// because the document.onload event doesn't fire until all images have loaded.
setupPage();
</script>
</body>
</html>
答案 1 :(得分:1)
优秀的代码,如果您还想更改复选框旁边标签的颜色,只需修改代码如下:
function setupPage() {
var agreement = document.getElementById("agreetext");
var visibleHeight = agreement.clientHeight;
var scrollableHeight = agreement.scrollHeight;
if (scrollableHeight > visibleHeight) {
var checkbox = document.getElementById("agreebox");
checkbox.checked=false;
checkbox.disabled=true;
document.getElementById("agreelabel").style.color = "#777";
agreement.onscroll = handleScroll;
}
}
function handleScroll() {
var agreement = document.getElementById("agreetext");
var visibleHeight = agreement.clientHeight;
var scrollableHeight = agreement.scrollHeight;
var position = agreement.scrollTop;
if (position + visibleHeight == scrollableHeight) {
document.getElementById("agreebox").disabled=false;
document.getElementById("agreelabel").style.color = "black";
}
}
答案 2 :(得分:1)
我使用了readonly textarea来显示我的许可协议。请注意,如果许可协议文本不够长,无法使textarea显示滚动条,则此代码将不起作用。
$(function () {
var serviceAgreementScrolled = false;
$('#service-agreement-textarea').scroll(
function () {
if (this.scrollTop + $(this).height() + 30 >= this.scrollHeight) {
serviceAgreementScrolled = true;
}
}
);
$('#accept-service-agreement-checkbox').change(
function () {
if ($(this).is(':checked') && !serviceAgreementScrolled) {
alert('Please scroll to read the rest of the service agreement.');
$(this).prop('checked', false);
}
}
);
});
<textarea id="service-agreement-textarea" readonly="readonly">Long long text here</textarea>
<label>
<input type="checkbox" id="accept-service-agreement-checkbox" />
I accept the terms of the service agreement
</label>