Friday 15 July 2011

jquery - 2 Javascripts not working with each other... (smooth scrolling & fade body) -


Dear StackWeaverFlower,

I have two javascript snippets that do not work properly together.

The first one is to remove the body on one click event in the navigation, then redirect to a different page. But it only works after clicking on a link that turns on 'javascript-2'

Here's the first code:

  // JavaScript -1 & lt; Script type = "text / javascript" & gt; $ ("A.transition"). Click (function) {event.preventDefault (); linkLocation = this.href; $ ("body"). FedOt (1000, redirect page);}); Function redirect page () {window.location = linkLocation; } & Lt; / Script & gt; 'Javascript-2' is second, which works in conjunction with 'jquery.easing.1.3.js' and makes an anchor a nice smooth scroll. Scrolling always works fine and it fires in all situations  

I do not know why, but it seems that, smooth scroll javascript causes other javascript to fail.

I am really keen to answer this short mystery.

Here is the second one:

  // javascript - 2 & lt; Script type = "text / javascript" & gt; $ (Function () {$ ('ul.navscroll a, #test a'). Bind ('click', function (event) {var $ anchor = $ (this); $ ('html, body'). ( ) .mate ({scrollTop: $ ($ anchor.attr ('href')) offset (). Top}, 1500, 'easeInOutExpo'); event.preventDefault ();});}); & Lt; / Script & gt;    

update your Javascript-1 code like this :

  // JavaScript-1 & lt; Script type = "text / javascript" & gt; $ (Function () {// it loads fire linkLocation = false; // defines the link to fire the location location $ ("a.transition"). (Function (event) {event.preventDefault (); LinkLocation = this .href; $ ("body"). FadeOut (1000, redirect page);) redirect function page () {window.location = linkLocation;}}); & Lt; / Script & gt;   

Here are two main things:

  1. $ (function () {...}); After the DOM the jQuery-event is completely filled
  2. var linkLocation must be defined before reaching it redirect page () < To delete the error in / code> -Method

    Javascript-2 (where it breaks javascript) update them like this:

      // JavaScript- 2 & lt; Script type = "text / javascript" & gt; $ (Function () ($ ('ul.navscroll a, #test a'). Bind ('click', function (event) {target = $ (this) .attr ('href'); // at the top Goal Offset = $ (Target) .offset (); // This returns an object (top: y, left: x} $ ('html, body'). Stop (). Animate ({scrollTop: offset.top} , 1500, 'EasyIntexpo'); event.preventDefault ();})}}); & lt; / script>   

    Now if you type href = '# test If you click on the link with ', then you will scroll the element with ID testing, for example your & lt; footer id =' test '& gt; .

No comments:

Post a Comment