当我单击锚时,我一直在尝试使用一个函数将我的页面滚动到页面的顶部。我的jQuery脚本看起来像:
$("a#drop-user-box").click(function()
{
console.log("foor"); //Would return foo value once
$("html, body").animate({scrollTop: 0}, "slow", function()
{
//console.log("foor"); Would return foo value twice?
// Code doesn't work when in here
// $(".drop-down").toggleClass('hidden');
// $(".drop-down input[type='e-mail']:first-of-type").focus();
});
// Code does work when in here
// $(".drop-down").toggleClass('hidden');
// $(".drop-down input[type='e-mail']:first-of-type").focus();
});在animate()函数之外并在其内部执行代码段有什么不同?当下拉列表的两行位于.animate()函数中时,为什么代码不能工作呢?请解释一下这是怎么回事。
提前谢谢你,
编辑:
我该怎么做呢?:
$("a#drop-user-box").click(function(e) {
e.preventDefault();
$("html, body").animate({
scrollTop: 0
}, "slow", function() {
$('.drop-down').show();
});
});.user-box .drop-down
{
padding: 15px 25.5px;
display: block;
border: 1px solid #aaa;
background-color: #fff;
}
.user-box .drop-down form
{
width: 250px;
}
.space
{
border: 1px solid #000;
height: 900px;
}<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="drop-down hidden">
<form action="" method="post">
<div class="form-group">
<input type="e-mail" class="form-control text-box single-line" name="E-mail" placeholder="E-mail adres">
</div>
<div class="form-group">
<input type="password" class="form-control text-box single-line" name="Wachtwoord" placeholder="Wachtwoord">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Bewaar login informatie
</label>
</div>
<input type="submit" class="btn btn-default" value="inloggen">
</form>
<a href="#">Wachtwoord vergeten?</a>
</div>
<div class="space">
</div>
<a href="#" id="drop-user-box">Foo</a>
发布于 2016-03-26 00:08:07
它不能工作的原因是因为.show()函数没有删除“隐藏”类名。.show() & .hide()函数在style="display:block“和style="display:none”属性和属性上操作。因此,您必须更改这行HTML:
<div class="drop-down hidden">对此:
<div class="drop-down" style="display:none">那就成功了。有关演示,请参见此小提琴。
https://stackoverflow.com/questions/36228741
复制相似问题