[英]Scroll to a particular div in jquery?
I have to scroll to a particular div onform submit (if there is any error). 我必须滚动到特定的div onform提交(如果有任何错误)。 But it goes to the top of the window without showing the error message.
但是它会转到窗口顶部,而不会显示错误消息。 Here is my code :
这是我的代码:
HTML HTML
<form name="createProperty" method="POST" onsubmit = "return submit_new_property()">
<select class="slt_box" name="property_type" id="property_type">
<option>Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</form>
JS JS
function submit_new_property() {
var property_type = $('#property_type').val();
if(property_type == 'Select' || property_type == '' || property_type == null) {
document.getElementById("basic_info_error_div").innerHTML="Choose Property Type";
document.getElementById("basic_info_error_div").style.display='block';
$.scrollTo( $('#basic_info_error_div'), 500);
return false;
}
else {
document.getElementById("basic_info_error_div").innerHTML = "";
document.getElementById("basic_info_error_div").style.display = 'none';
}
}
You can use .scrollTop() 您可以使用.scrollTop()
$('html, body').animate({
'scrollTop' : $('#basic_info_error_div').position().top
}, 500);
OR 要么
If you want vanilla JS, You can use Element.scrollIntoView 如果您想要香草JS,则可以使用Element.scrollIntoView
document.getElementById('basic_info_error_div').scrollIntoView(true);
$('html, body').animate({
scrollTop: $("#basic_info_error_div").offset().top
}, 500);
You seem to be trying to use the jquery.scrollTo plugin. 您似乎正在尝试使用jquery.scrollTo插件。 You need do download it from the releases page .
您需要从发布页面下载它。
After that you can do: 之后,您可以执行以下操作:
$.scrollTo('#basic_info_error_div', 500);
And it will work as expected. 它将按预期工作。
Cheers 干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.