HTML anchor tag with Javascript onclick event

If your onclick function returns false the default browser behaviour is cancelled. As such: <a href=”http://www.google.com” onclick=’return check()’>check</a> <script type=”text/javascript”> function check() { return false; } </script> Either way, whether google does it or not isn’t of much importance. It’s cleaner to bind your onclick functions within javascript – this way you separate your HTML … Read more

Opening tab with anchor link

You could solve this by simply checking the hash when the page loads, and then trigger a click on the right tab, like so: $(function () { $(“.tab-content”).hide().first().show(); $(“.inner-nav li:first”).addClass(“active”); $(“.inner-nav a”).on(‘click’, function (e) { e.preventDefault(); $(this).closest(‘li’).addClass(“active”).siblings().removeClass(“active”); $($(this).attr(‘href’)).show().siblings(‘.tab-content’).hide(); }); var hash = $.trim( window.location.hash ); if (hash) $(‘.inner-nav a[href$=”‘+hash+'”]’).trigger(‘click’); });

ScrollTo function in AngularJS

Here is a simple directive that will scroll to an element on click: myApp.directive(‘scrollOnClick’, function() { return { restrict: ‘A’, link: function(scope, $elm) { $elm.on(‘click’, function() { $(“body”).animate({scrollTop: $elm.offset().top}, “slow”); }); } } }); Demo: http://plnkr.co/edit/yz1EHB8ad3C59N6PzdCD?p=preview For help creating directives, check out the videos at http://egghead.io, starting at #10 “first directive”. edit: To make it … Read more

Using HTML anchor link fragment in Angular 6

Angular 6.1 comes with an option called anchorScrolling that lives in router module’s ExtraOptions. As the anchorScrolling definition says: Configures if the router should scroll to the element when the url has a fragment. ‘disabled’ — does nothing (default). ‘enabled’ — scrolls to the element. This option will be the default in the future. Anchor … Read more

Smooth scroll anchor links WITHOUT jQuery

Extending this answer: https://stackoverflow.com/a/8918062/3851798 After defining your function of scrollTo, you can pass the element you want to scrollTo in the function. function scrollTo(element, to, duration) { if (duration <= 0) return; var difference = to – element.scrollTop; var perTick = difference / duration * 10; setTimeout(function() { element.scrollTop = element.scrollTop + perTick; if (element.scrollTop … Read more