[英]How to slide page when link is clicked in Jquery
基本上,我有一個頁面滾動網站,我想在按下鏈接時滾動頁面。 到目前為止,這是我的html:
<div id="container">
<div id="inner">
<div id="home">
<button>Click</button>
<div class = "Category_Menu_Container">
<a class ="menu_Items" href="" >+ Events</a>
<a class ="menu_Items" href="" >+ News</a>
<a class ="menu_Items" href="" >+ Info</a>
<a class ="menu_Items" href="" >+ Sports</a>
</div>
<p> Choose a Category </p>
</div>
<div id="Events_Page">
<button>Click</button>
</div>
<div id="News_Page">
<button>Click</button>
</div>
</div>
</div>
這是我的jQuery:
function toggleDivs() {
var $inner = $("#inner");
// See which <divs> should be animated in/out.
if ($inner.position().left == 0) {
$inner.animate({
left: "-100%"
});
}
else {
$inner.animate({
left: "0px"
});
}
}
$(document).ready(function() {
$('.menu_Items').bind("click", function() {
alert("Got here");
});
$("button").bind("click", function() {
toggleDivs();
});
});
當我單擊“事件”鏈接或任何鏈接,僅適用於“單擊”按鈕時,這不起作用,我希望能夠單擊任何鏈接以轉到下一個視圖,而不僅僅是單擊“我”。 謝謝很多人!
嘗試更改此:
$("button").bind("click", function() {
toggleDivs();
});
對此:
$(document).on("click", "button", function() {
toggleDivs();
});
如果不是動態創建按鈕,則可以更輕松地執行操作:
$("button").click(function() {
toggleDivs();
});
更新
超鏈接的相同之處:
$(document).on("click", ".menu_Items", function() {
alert("Got here");
});
或更容易:
$(".menu_Items").click(function() {
alert("Got here");
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.