admin管理员组文章数量:1278823
I have the following div for a modal box -
<div class="modal fade" id="Community" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-content">
<button type="button" onclick="window.location.href='#Pricing'" data-dismiss="modal">Close and go to pricing</button>
</div>
</div>
On click of the button the modal window closes but it doesn't go to the correct section indicated by #Pricing.
I have the following div for a modal box -
<div class="modal fade" id="Community" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-content">
<button type="button" onclick="window.location.href='#Pricing'" data-dismiss="modal">Close and go to pricing</button>
</div>
</div>
On click of the button the modal window closes but it doesn't go to the correct section indicated by #Pricing.
Share Improve this question edited Nov 25, 2014 at 4:41 jacksparrow007 asked Nov 25, 2014 at 4:30 jacksparrow007jacksparrow007 1,3284 gold badges21 silver badges31 bronze badges 3- 1 why don't you use <a> link – rjdmello Commented Nov 25, 2014 at 4:37
- @rjdmello is right. Use a normal link, there is no need to close the modal when you go to another page anyway... – Dominik Commented Nov 25, 2014 at 4:40
- 1 I have tried that and it behaves in the same way. And the link is not for another page. Its for the same page with a div with id="#Pricing" . It doesn't go to the required section. Just closes the modal and gets stuck. – jacksparrow007 Commented Nov 25, 2014 at 4:41
4 Answers
Reset to default 5You are missing a '
in onclick
.
<button type="button" onclick="window.location.href='#Pricing'" data-dismiss="modal">Close and go to pricing</button>
Update
I think I get to know the reason. Actually, in your case, onclick
is fired before data-dismiss="modal"
. This means, the location change would happen even before the modal dismissal. However, in order to prevent the page from scroll when a modal present, the body will have a modal-open
class. This takes overflow: hidden;
, so the page will not be able to scroll more than the height of the modal itself.
To solve this problem, you need to defer your location change to after the modal disappears. You can move it into the callback of the hidden.bs.modal
event.
The event docs can be found here: http://getbootstrap./javascript/#modals-usage .
Here's how to close the dialog and go to the document fragment at the same time:
<a href="#foobar">
<span data-bs-dismiss="modal">Close</span>
</a>
if the link is in same page just use
jQuery(function($) {
$('#myModal').on('hidden.bs.modal', function (e) {
$('html, body').animate({
scrollTop: $("#pricing").offset().top
}, 2000);
})
});
see this jsfiddle
use this event
$('#Community').on('hidden.bs.modal', function (e) {
// do something...
window.location.href='/#Pricing'
})
本文标签: javascriptCSS Bootstrap close modal and go to linkStack Overflow
版权声明:本文标题:javascript - CSS Bootstrap close modal and go to link - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741223727a2361463.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论