javascript - Element disappearing on click -
i'm trying things jquery , there seems going wrong.
first off code,
my html,
<a href="#" onclick="return false;" class="scrolltotop"></a>
my css,
.scrolltotop { width: 40px; height: 40px; line-height: 45px; border-radius: 50%; text-align: center; background-color: #cfcfcf; background-image: url(../img/upactive.png); background-repeat: no-repeat; background-position: 50% 50%; position: fixed; bottom: 61px; right: 30px; transition: opacity 2s ease-in; } .scrolltotop:hover { background-color: #989898; text-decoration: none; transition: opacity 2s ease-in; }
my jquery,
$(document).ready(function() { var hidden = $('.scrolltotop'); hidden.hide(); // check see if window top if not display button $(window).scroll(function() { if ($(this).scrolltop() > 100) { hidden.show(1000); } else { hidden.hide(1000); } }); // click event scroll top $('.scrolltotop').click(function(e) { $('html, body').animate({ scrolltop: 0 }, 800); return false; }); });
jsfiddle: fiddle (thanks vasilenicusor)
the problem buttons in project dissapear after have been clicked. can see ring arround them browser puts there, fysical button dissapears.. problem not seem happen in fiddle (the code not working @ in fiddle).
does know how should fix , maby problem? guess in jquery code making buttons this. (there 8 jquery files 100+ lines of code, of them library's).
the code below not work. i've tried it.
(function(e) { e.preventdefault; });
thanks in advance!
thanks chun answer! seems problem in code there seems library causing "dissapear" effect when there "href" link. removing "href" temporally solved problem.
you use href="javascript:void(0)"
instead of href="#" onclick="return false;"
<a href="javascript:void(0)" class="scrolltotop"></a> <!-- or better --> <a class="scrolltotop"></a> <!-- or better --> <span class="scrolltotop"></span>
and give scrolltotop
attributes of link css markup without href
, like:
.scrolltotop { cursor: pointer; color: blue; }
having <a href="#" onclick="myjsfunc();">link</a>
or <a href="javascript:void(0)" onclick="myjsfunc();">link</a>
or whatever else contains onclick
attribute - okay couple of years ago, though can bad practice. here's why:
it promotes practice of obtrusive javascript - has turned out difficult maintain , difficult scale. more on in unobtrusive javascript.
there better, easier, , more maintainable , scalable ways of accomplishing desired result.
the unobtrusive javascript way
just don't have href
attribute @ all! css reset take care of missing default cursor style, non-issue. in case have javascript functionality attach using graceful , unobtrusive best practices - more maintainable javascript logic stays in javascript, instead of in markup - essential when start developing large scale javascript applications.
<a class="scrolltotop">cancel action</a> // cancel click event $('.scrolltotop').click(function(){ alert('cancel action occurs!'); }); .scrolltotop { cursor: pointer; color: blue; } .scrolltotop:hover { text-decoration: underline; }
update
i've forget say, change scroll top function instead:
// click scroll top $(".scrolltotop").click(function() { $("html, body").animate({ scrolltop: 0 }, "slow"); });
check out jsfiddle example here: https://jsfiddle.net/x1x9pmpx
Comments
Post a Comment