using chrome and ff so far have made a nasty glitch when using scrollTo function? anyone know how to prevent this from happening?
Live Code
HTML
<ul class="wrapper">
<li class="listItem"><img src="asset/img/logo.png" ></li>
<li class="listItem"><a onclick="scrollTo('#container',500);" href="#">Company</a><span> / </span></li>
<li class="listItem"><a onclick="scrollTo('.flexslider-container',500);" href="#">Service</a><span> / </span></li>
<li class="listItem"><a onclick="scrollTo('#map',500);" href="#">Map</a><span> / </span></li>
<li class="listItem"><a onclick="scrollTo('#footer',500);" href="#">Contact</a></li>
<li class="listItem">
<p>250-610-9100</p>
</li>
</ul>
SCRIPT
<script type="text/javascript">
function scrollTo(o, s) {
var d = $(o).offset().top;
$("html:not(:animated),body:not(:animated)").animate({
scrollTop: d
}, s);
}
</script>
You should be attaching events via
.on(), not shoving code into theonclickattribute.The (more) proper solution is to give you links a
class:And then some fancy
dataattributes:And in your code, add this:
Basically, you give your elements special attributes that can be read with jQuery’s
.data()function. Then, instead of adding a function call to each link, you attach one magic function to all of them.If you want to keep your current code (ehh), just add
return false;at the end of everyonclickattribute:This prevents the default action from occurring (scrolling to the top of the page).
I recommend my first solution. You aren’t doing jQuery any justice if you are only using it to a third of its potential.