javascript - jquery scroll and click outside of the div and hide the div? -
i creating effect. when user click div check, scroll #basket-page , show, , when user click outside, #basket-page hide. issue when user click div check first time, #basket-page show , hide immediately, if click second time, or third time, has no problem. can fix this.
jquery(document).mouseup(function (e) { var container = jquery("#basket-page"); if (!container.is(e.target) // if target of click isn't container... && container.has(e.target).length === 0) // ... nor descendant of container { container.fadeout(); } }); jquery('#checkout_top').click(function(){ var basket_page=jquery('#basket-page'); jquery('#top_basket').html(basket_page); var position = jquery("#myshop_wrap").position(); scroll(0,position.top); jquery("#basket-page").show(); });
<div id="#checkout_top">check</div> <div id="basket-page"></div>
if understand question correctly want check div can click on , show , scroll basket-page div. believe correct way not use scroll() function animate top of body top of #basket-page div
this should work
jquery(document).mouseup(function (e) { var container = jquery("#basket-page"); var checkoutcontainer = jquery('#checkout-top'); if (!container.is(e.target) // if target of click isn't container... && container.has(e.target).length === 0 // ... nor descendant of container && !checkoutcontainer.is(e.target)) //nor checkout box { container.fadeout(1000); } }); jquery('#checkout-top').click(function () { jquery('#basket-page').show(); jquery('html, body').animate({ scrolltop: jquery("#basket-page").offset().top }, 1600); });
check out jsfiddle added css colors , make code work. added little part in mouseup event, includes check if click occurs in checkout div well.
nb: encourage use dash '-' instead of underscore '_' in class , id names. link discussion on
hope helped
Comments
Post a Comment