Results 1 to 8 of 8

Thread: conflict tab and smooth scroll scripts

  1. #1
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default conflict tab and smooth scroll scripts

    Hey all.
    There seems to be a conflict between these two scripts on the same page.

    This is the tab script

    Code:
    $('#myTabs a').click(function (e) {
    	e.preventDefault()
    	$(this).tab('show')
    })
    And this is the smooth scroll script

    Code:
    $('a[href*=#]:not([href=#])').click(function() {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
            || location.hostname == this.hostname) {
    
            var target = $(this.hash);
            target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
               if (target.length) {
                 $('html,body').animate({
                     scrollTop: target.offset().top
                }, 1000);
                return false;
            }
        }
    });
    Can anyone help me out so I can have the two scripts running on the same page, please?
    Thanks!

  2. #2
    Join Date
    Jan 2009
    Location
    NH
    Posts
    675
    Thanks
    98
    Thanked 26 Times in 26 Posts

    Default

    do they use the same javascript version?

  3. #3
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default

    Hey, thanks for replying.
    I would suppose they do, as both are uploaded on the same webspace, trying to run it at the same page.
    The tabs code works; the smooth scroll is what I'd like to add. So I found the smoothscroll code online, trying to implement it.
    But then the tabs stopped working.

  4. #4
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    The second script appears to be overriding the first as both are using <a> tags.

    It's difficult to know without seeing your real page, so this is just a guess. You could try removing all the <a> tags in the section with the id "myTabs" and replace them with a set of <div> or <span> tags with a common classname such as "tabsToOpen", then change the first script to the following:

    Code:
    $('#myTabs .tabsToOpen').click(function (e) {
    	e.preventDefault()
    	$(this).tab('show')
    })

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Not enough there to tell really. What's the href of a typical tab? If it's a hash, that might be the trouble. If it is and doesn't need to be, make it something else. But if it has to be a hash, and if that's the problem, you pretty much would have to change one of the scripts, probably in a major way.

    But as I say, I can't tell from that little bit of code.

    Can you provide a link to the page with both scripts on it, even if only one of them works?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default

    Hey John, this is the url
    You an see three buttons "houtsoorten" "beglazing" "deuren" beneath the header visual: they perfectly work well with smoothscroll
    But at the bottom of the page, there's the tab content "onze zes pijlers" (expertise, opvolging, afspraken) that chokes.

    Here's the code of the tabs (line 81) and the smoothscroll.js
    Hope you can work it out.
    Thanks.

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Well let's see - that's not the tab code. There are no $('#myTabs a'). But there does appear to be a conflict. Focus only on the 9 scroll links for the scroll initialization. Use this (three additions/changes - red) instead of the current smoothscroll.js:

    Code:
    $('a.drill-btn-tab[href*=#]:not([href=#])').click(function(e) {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
            || location.hostname == this.hostname) {
    
            var target = $(this.hash);
            target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
               if (target.length) {
                 $('html,body').animate({
                     scrollTop: target.offset().top
                }, 1000);
                e.preventDefault();
            }
        }
    });
    The browser cache may have to be cleared and/or the page refreshed to see changes.



    Notes: The preventDefault() method is preferable to return false as it will not prevent other events from working on these elements (that was the main problem). However, assigning both behaviors to the bottom tabs has other undesirable consequences. USE ONLY ONE FIX OR THE OTHER FOR THIS, but alternatively one could eliminate the other hash links (might be preferable if other scroll links with different class name(s) are used in the future):

    Code:
    $('a[href*=#]:not(.service-name, [href=#])').click(function(e) {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
            || location.hostname == this.hostname) {
    
            var target = $(this.hash);
            target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
               if (target.length) {
                 $('html,body').animate({
                     scrollTop: target.offset().top
                }, 1000);
                e.preventDefault();
            }
        }
    });
    Last edited by jscheuer1; 07-11-2016 at 04:31 PM. Reason: add about preventDefault
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. The Following User Says Thank You to jscheuer1 For This Useful Post:

    chechu (07-12-2016)

  9. #8
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default

    Works perfectly, thanks!!

Similar Threads

  1. Conflict of scripts? Too many?
    By oldmanInAz in forum Dynamic Drive scripts help
    Replies: 4
    Last Post: 06-06-2012, 10:50 PM
  2. Conflict with scripts
    By Burgin in forum JavaScript
    Replies: 6
    Last Post: 10-05-2009, 04:05 PM
  3. Conflict between to scripts
    By sandra in forum JavaScript
    Replies: 1
    Last Post: 03-07-2009, 07:03 AM
  4. Conflict between two scripts!
    By chrbar in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 01-08-2008, 07:52 PM
  5. some conflict between two scripts of DD
    By two4one in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 10-25-2005, 07:43 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •