Topic: Can't figure out links in nav
Steve Stacha pro asked 7 years ago
Steve Stacha pro answered 7 years ago
//SMOOTH SCROLL
$(".smooth-scroll").on('click', 'a', function(event) {
event.preventDefault();
var elAttr = $(this).attr('href');
var offset = ($(this).attr('data-offset') ? $(this).attr('data-offset') : 0);
var setHash = $(this).closest('ul').attr('data-allow-hashes');
$('body,html').animate({
scrollTop: $(elAttr).offset().top - offset
}, 700);
if (typeof setHash !== typeof undefined && setHash !== false) {
history.replaceState(null, null, elAttr);
}
});
Replace that code with this code which checks to make sure it is an anchor first otherwise does nothing:
//SMOOTH SCROLL
$(".smooth-scroll").on('click', 'a', function(event) {
var elAttr = $(this).attr('href');
var offset = ($(this).attr('data-offset') ? $(this).attr('data-offset') : 0);
var setHash = $(this).closest('ul').attr('data-allow-hashes');
// sas 12.02.17 : if the elAttr is a # we want to do this; normal links need to skip (redirect to other page)
if (typeof elAttr !== 'undefined' && elAttr.startsWith('#')) {
event.preventDefault();
$('body,html').animate({
scrollTop: $(elAttr).offset().top - offset
}, 700);
if (typeof setHash !== typeof undefined && setHash !== false) {
history.replaceState(null, null, elAttr);
}
}
});
I will post a suggestion to include this line in the base code. If anyone chooses to do this they will need to run minification and replace the mdb.min.js in the js folder. I hope this weekend of debugging helps someone. Jakub Strebeyko staff answered 7 years ago
smooth-scroll
indeed, as it cannot "smooth-scroll" you into a separate webpage. To have both same page smooth-scroll and regular, resourced outside page links in your navbar, please consider moving smooth-scroll
CSS class from the navbar itself to respective li
elements hosting links to properly id
ed elements of your page.
With Best Regards,
Kuba Steve Stacha pro commented 7 years ago
I went ahead and moved the about us block in the footer to a different page so I could just remove the smooth-scroll altogether. You might still consider adding a check beforehand anyway. I can't think of why you wouldn't want to skip the scrolling on anything that is not an anchor (starts with a #) and I wasn't the only one confused when using the templates we bought. I think people copying one of your pages and slowly trying to switch out links in the header as they create new pages will be a common occurrence. The less changes to tags we have to remember to make the better.Jakub Strebeyko staff commented 7 years ago
It's all true, Steve. With the help of people like you we get new insights we use to improve MDB. Thanks again!Closed
This topic is closed.
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Closed
- User: Pro
- Premium support: No
- Technology: MDB jQuery
- MDB Version: -
- Device: -
- Browser: -
- OS: -
- Provided sample code: No
- Provided link: No