2017-02-28 1 views
1

Ich erstellte einfache Link-Animation, um zu blättern, kann dieser Link sein, um im Abschnitt auf der gleichen Seite oder Link zu externen URL zu blättern, das Problem, dass wenn Sie auf die Schaltfläche ("Abschnitt 4") klicken finde eine seltsame Bewegung, das war das erste Problem.
Scrollen, um Animation


Zweites Problem: dass, wenn der Benutzer klicken Sie auf die Schaltfläche auf eine andere Schaltfläche klicken Sie einmal mehr als dann wird die Scroll nicht funktionieren, bis die vorherige Klick beendet:
ist mein Code unten:

$(".links a").click(function() { 
 
      $("html, body").animate({ 
 
       scrollTop: $($(this).attr("href")).offset().top 
 
      }, 1400) 
 
     })
.links { 
 
    width:600px; 
 
    position:fixed; 
 
    top:0; 
 
    padding:20px; 
 
} 
 
.links a { 
 
    display:inline-block; 
 
    padding:10px 20px; 
 
    border:1px solid #0094ff; 
 
} 
 
.section { 
 
    width:400px; 
 
    height:200px; 
 
    margin:300px auto 600px; 
 
    background-color:#0094ff; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="links"> 
 
     <a href="#section1">Section 1</a> 
 
     <a href="#section2">Section 2</a> 
 
     <a href="#section3">Section 3</a> 
 
     <a href="#section4">Section 4</a> 
 
     <a href="http://google.com">External Link</a> 
 
    </div> 
 
    <div id="section1" class="section"></div> 
 
    <div id="section2" class="section"></div> 
 
    <div id="section3" class="section"></div> 
 
    <div id="section4" class="section"></div>

Hinweis: rate mir bitte nicht, irgendein plugin zu benutzen, das ich mehr und mehr über Javascript wissen möchte

+0

nicht sicher, ob ich verstehe, vor allem, was mit einem Klick auf '# section4' falsch ist, aber tut diese Adresse, die er oder die zweite Ausgabe ? Klingt so, als müssten Sie '$ .stop()' vor '$ .animate()' 'http://codepen.io/anon/pen/LWpeyJ –

+0

aufrufen. Normalerweise können Sie das zweite Problem, das Sie erwähnen, mit' .stop beheben() 'so würde Ihr Code lesen $ (" html, body "). stop(). animate ({...' – Djave

+0

Vielen Dank aber über das erste Problem :) –

Antwort

4

Sie können e.preventDefault() verwenden.

Führen Sie das Ereignisargument in dem Click-Ereignis durch Ihre Funktion ändert:

$(".links a").click(function() { 

zu

$(".links a").click(function (e) { 

Jetzt können Sie das Standardereignis des Klick bearbeiten. Sie können dies mit preventDefaulthttps://developer.mozilla.org/en/docs/Web/API/Event/preventDefault tun.

Zweitens können Sie stop() verwenden, um die Animation zu beheben.

$(".links a").click(function (e) { 
 
    if ($(this).attr("href").charAt(0) == "#") { 
 
    e.preventDefault(); 
 
    $("html, body").stop().animate({ 
 
     scrollTop: $($(this).attr("href")).offset().top 
 
    }, 1400) 
 
    } 
 
});
.links{ 
 
       width:600px; 
 
       position:fixed; 
 
       top:0; 
 
       padding:20px; 
 
      } 
 
      .links a{ 
 
       display:inline-block; 
 
       padding:10px 20px; 
 
       border:1px solid #0094ff; 
 
      } 
 
      .section{ 
 
       width:400px; 
 
       height:200px; 
 
       margin:300px auto 600px; 
 
       background-color:#0094ff; 
 
      }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="links"> 
 
     <a href="#section1">Section 1</a> 
 
     <a href="#section2">Section 2</a> 
 
     <a href="#section3">Section 3</a> 
 
     <a href="#section4">Section 4</a> 
 
     <a href="http://google.com">External Link</a> 
 
    </div> 
 
    <div id="section1" class="section"></div> 
 
    <div id="section2" class="section"></div> 
 
    <div id="section3" class="section"></div> 
 
    <div id="section4" class="section"></div>

+0

mit preventDefault wird verhindern, öffnen Sie den externen Link –

+0

I Schlage vor, dass die href mit do preventDefault startet Aber ich weiß nicht, wie ich das mache –

+0

Hey @MichaelNeas Ich bin mir sicher, dass du das jetzt herausgefunden hast, aber du kannst überprüfen, ob ein Link href ganz leicht mit # beginnt . Wie all Ihre internen Links tun, können wir die Funktion nur auf diesen Links auslösen, indem wir alles in 'if ($ (this) .attr (" href "). CharAt (0) ==" # ") {' einschließen. Das heißt, wenn das href-Attribut von diesem (das angeklickte Element) am Anfang ein Zeichen hat, das ein Hash ist, mache folgendes. – Djave

0

können Sie verwenden jQuery stop() Methode

$(".links a").click(function (e) { 
 
    var hrefLink = $(e.target).attr("href"); 
 
    var hrefSplitted = hrefLink.split("#"); 
 
    if(hrefSplitted.length == 2){ 
 
    e.preventDefault(); 
 
    }; 
 
      /* if(! $(e.target).is('.links a:last')){ 
 
       e.preventDefault(); 
 
      }*/ 
 
      
 
      $("html, body").stop(); 
 
      $("html, body").animate({ 
 
       scrollTop: $($(this).attr("href")).offset().top 
 
      }, 1400) 
 
     })
.links{ 
 
       width:600px; 
 
       position:fixed; 
 
       top:0; 
 
       padding:20px; 
 
      } 
 
      .links a{ 
 
       display:inline-block; 
 
       padding:10px 20px; 
 
       border:1px solid #0094ff; 
 
      } 
 
      .section{ 
 
       width:400px; 
 
       height:200px; 
 
       margin:300px auto 600px; 
 
       background-color:#0094ff; 
 
      }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="links"> 
 
     <a href="#section1">Section 1</a> 
 
     <a href="#section2">Section 2</a> 
 
     <a href="#section3">Section 3</a> 
 
     <a href="#section4">Section 4</a> 
 
     <a href="http://google.com">External Link</a> 
 
    </div> 
 
    <div id="section1" class="section" style="background:red;"></div> 
 
    <div id="section2" class="section" style="background:blue;"></div> 
 
    <div id="section3" class="section" style="background:green;"></div> 
 
    <div id="section4" class="section" style="background:yellow;"></div>

+0

mit preventDefault wird verhindert, externe Verbindung zu öffnen –

+0

Ich schlage vor, dass überprüfen, ob die href mit do preventDefault ist Aber ich weiß nicht, wie das geht –

+0

Danke für das Zeigen, dass ich eine Überprüfung dafür hinzugefügt haben, Überprüfen, ob es das letzte ist Anker-Tag. Aktualisiert Snippet – XYZ

0

Sie e.preventDefault() Methode verwenden können, in diesem Beispiel verhindern e.preventDefault() wird die Verbindung von im Anschluss an die URL verhindern .

$(".links a").click(function (e) { 
    e.preventDefault(); 

}); 

Aber e.preventDefault() Es wird ein anderes Problem verursachen, weil, wenn Sie externen Link haben Sie nicht in der Lage sein, es zu öffnen, so was ist die Lösung, die Lösung Die Lösung ist, den Wert von href zu testen, wenn die href beginnen mit "#" Anruf e.preventDefault():

$(".links a").click(function (e) { 
    if ($(this).attr("href").charAt(0) == "#") { 
     e.preventDefault(); 
    } 
}); 

Sie auch, wenn der Link externer Link ist zu überprüfen, in neuem Tab öffnen:

$(".links a").click(function (e) { 
    if ($(this).attr("href").charAt(0) == "#") { 
     e.preventDefault(); 
    } 
    else { 
     $($(this)).attr("target","_blank") 
    } 
}); 


Schließlich können Sie mehrere Klicks mit stop() Methode verhindern.

Hier ist der endgültige Code:

$(".links a").click(function (e) { 
 
      if (this.getAttribute("href").charAt(0) == "#") { 
 
       e.preventDefault(); 
 
       $("html, body").stop(); 
 
       $("html, body").animate({ 
 
        scrollTop: $($(this).attr("href")).offset().top 
 
       }, 1400) 
 
      } 
 
      else { 
 
      $($(this)).attr("target","_blank") 
 
     } 
 
     })
.links{ 
 
    width:600px; 
 
    position:fixed; 
 
    top:0; 
 
    padding:20px; 
 
} 
 
.links a{ 
 
    display:inline-block; 
 
    padding:10px 20px; 
 
    border:1px solid #0094ff; 
 
} 
 
.section{ 
 
    width:400px; 
 
    height:200px; 
 
    margin:300px auto 600px; 
 
    background-color:#0094ff; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
 
<div class="links"> 
 
    <a href="#section1">Section 1</a> 
 
    <a href="#section2">Section 2</a> 
 
    <a href="http://google.com" target="_blank">External Link</a> 
 
    <a href="#section3">Section 3</a> 
 
    <a href="#section4">Section 4</a> 
 
</div> 
 
<div id="section1" class="section"></div> 
 
<div id="section2" class="section"></div> 
 
<div id="section3" class="section"></div> 
 
<div id="section4" class="section"></div>

+0

Vielen Dank es ist, was ich brauche Nochmals vielen Dank MoHamed –

+0

Ihre Antwort ist unvollständig.Sie sollten Erläuterungen zu Ereignis sprudeln und jQuery-Animation-Warteschlangen geben. https://api.jquery.com/event.preventdefault/ // https://www.w3.org/TR/DOM-Level-2-Events/events.html // https://api.jquery.com/event.preventdefault/ // https://learn.jquery.com/effects/queue-and-dequeue-explained/ // https://api.jquery.com/stop/ // https://api.jquery.com/clearQueue/ –

+0

Vielen Dank, aber mu Grundsprache ist nicht Englisch Sie können meine Antwort bearbeiten –

Verwandte Themen