2012-07-19 107 views
0

我有兩個腳本。一種是使用#idname滾動到頁面的特定部分,另一種是交換內容,也使用#idname衝突的腳本#selector標籤

所有的id都是唯一的,它們在不同的容器div中,但是當我單擊交換內容時,也會觸發scroller腳本。

這裏有兩個腳本是相互矛盾的:

滾動腳本:

(function ($) { 
    var l = location.href.replace(/#.*/, ''); 
    var g = $.localScroll = function (a) { 
      $('body').localScroll(a) 
     }; 
    g.defaults = { 
     duration: 1e3, 
     axis: 'y', 
     event: 'click', 
     stop: true, 
     target: window, 
     reset: true 
    }; 
    g.hash = function (a) { 
     if (location.hash) { 
      a = $.extend({}, g.defaults, a); 
      a.hash = false; 
      if (a.reset) { 
       var e = a.duration; 
       delete a.duration; 
       $(a.target).scrollTo(0, a); 
       a.duration = e 
      } 
      i(0, location, a) 
     } 
    }; 
    $.fn.localScroll = function (b) { 
     b = $.extend({}, g.defaults, b); 
     return b.lazy ? this.bind(b.event, function (a) { 
      var e = $([a.target, a.target.parentNode]).filter(d)[0]; 
      if (e) i(a, e, b) 
     }) : this.find('a,area').filter(d).bind(b.event, function (a) { 
      i(a, this, b) 
     }).end().end(); 

     function d() { 
      return !!this.href && !! this.hash && this.href.replace(this.hash, '') == l && (!b.filter || $(this).is(b.filter)) 
     } 
    }; 

    function i(a, e, b) { 
     var d = e.hash.slice(1), 
      f = document.getElementById(d) || document.getElementsByName(d)[0]; 
     if (!f) return; 
     if (a) a.preventDefault(); 
     var h = $(b.target); 
     if (b.lock && h.is(':animated') || b.onBefore && b.onBefore.call(b, a, f, h) === false) return; 
     if (b.stop) h.stop(true); 
     if (b.hash) { 
      var j = f.id == d ? 'id' : 'name', 
       k = $('<a> </a>').attr(j, d).css({ 
        position: 'absolute', 
        top: $(window).scrollTop(), 
        left: $(window).scrollLeft() 
       }); 
      f[j] = ''; 
      $('body').prepend(k); 
      location = e.hash; 
      k.remove(); 
      f[j] = d 
     } 
     h.scrollTo(f, b).trigger('notify.serialScroll', [f]) 
    } 
})(jQuery); 

內容衰落:

$(window).load(function() { 
    (function($) { 
     $.fn.Fader = function() { 
      this.each(function() { 
       $('a').bind('click', function(e) { 
        e.preventDefault(); 
        $("#mediaswap div").fadeOut(); 
        $("#mediaswap div" + $(this).attr('href')).fadeIn(); 
       }) 
      }); 
     } 
    })(jQuery); 
    $(function() { 
     $('#mediaswap').Fader(); 
    }); 
}); 

什麼我需要更改腳本2(內容衰落)所以他們不衝突?

+0

你不應該擴展jQuery的原型的東西,只有一個頁面相關 – Esailija 2012-07-19 15:50:21

回答

0

我似乎已經找到了解決辦法,買改變

$(this).attr('href')).fadeIn(); 

$(this).attr('name')).fadeIn();