animation - jQuery animate/scroll only partially works on second click -


when use jquery animate scrolling behavior first click cause view jump straight clicked anchor element. second click, if issued within animation-delay, work flawlessly. if animation-delay runs out , 1 issues click again, it'll cause straight jump element again.

here's short gif demonstrating issue: enter image description here

as can see, click on services , instantly jump. wait until animation delay runs off , click portfolio, again instant jump. click services again (animation delay didn't run off yet) , works expected.

my elm-application has onclick events assigned elements. when triggered, href of element gets passed javascript function through port, so:

-- partial view view =     div []         [ nav [ class "navbar navbar-default navbar-fixed-top" ]             [ div [ class "container" ]                 [ div [ class "navbar-header page-scroll" ]                     [ button [ class "navbar-toggle", attribute "data-target" "#bs-example-navbar-collapse-1", attribute "data-toggle" "collapse", type' "button" ]                         [ span [ class "sr-only" ]                             [ text "toggle navigation" ]                         , span [ class "icon-bar" ]                             []                         , span [ class "icon-bar" ]                             []                         , span [ class "icon-bar" ]                             []                         ]                     , [ class "navbar-brand page-scroll", href "#page-top", onclick (update.clickedelem "#page-top") ]                         [ text "start bootstrap" ]                     ]                 , div [ class "collapse navbar-collapse", id "bs-example-navbar-collapse-1" ]                     [ ul [ class "nav navbar-nav navbar-right" ]                         [ li [ class "hidden" ]                             [ [ href "#page-top" ]                                 []                             ]                         , li []                             [ [ class "page-scroll", href "#services", onclick (update.clickedelem "#services") ]                                 [ text "services" ]                             ]                         , li []                             [ [ class "page-scroll", href "#portfolio", onclick (update.clickedelem "#portfolio") ]                                 [ text "portfolio" ]                             ]                         , li []                             [ [ class "page-scroll", href "#about", onclick (update.clickedelem "#about") ]                                 [ text "about" ]                             ]                         , li []                             [ [ class "page-scroll", href "#team", onclick (update.clickedelem "#team") ]                                 [ text "team" ]                             ]                         , li []                             [ [ class "page-scroll", href "#contact", onclick (update.clickedelem "#contact") ]                                 [ text "contact" ]                             ]                         ]                     ]                 , text "  "                 ]             ]         , section [ id "services" ]             [-- more content             ]           -- more content         , section [ id "contact" ]             [-- more content             ]         ]  -- partial update  case msg of     clickedelem elem ->         -- sends clicked element '#services' js world         ( model, clickedelem elem )      changelocation newlocation ->         ( { model             | location = newlocation           }         , cmd.none         )   -- port port clickedelem : string -> cmd msg port changeloc : (string -> msg) -> sub msg   -- subscription subscriptions model =     sub.batch         [ window.resizes update.resize         , update.changeloc update.changelocation         ] 

on js-side use jquery animate / scrollto function, so:

<script>   app.ports.clickedelem.subscribe(function(id){     console.log("scrolling.");     // why isn't working properly?     $('html, body').animate({       scrolltop: $(id).offset().top     }, 2000);     // return false;     app.ports.changeloc.send(id);   }); </script> 

finally new location get's passed elm through port called changeloc updates model.

what missing here? problem due virtual-dom of elm? can't seem figure out. offsets of provided anchors fine , function get's called it's supposed to.


here's way fix issue: instead of using onclick used tip @chadgilbert , went onwithoptions set preventdefault true. outcome:

onwithoptions "click" { stoppropagation = true, preventdefault = true } (json.succeed (update.clickedelem "#anchor")) 

instead of

onclick (update.clickedelem "#anchor") 

thanks!

the links in header created onclick , since you're setting href parameter, event bubble handled browser, why getting immediate jump.

in javascript, way disable default browser behaviors , event propagation use event.stoppropagation() , event.preventdefault().

in elm, can create event attributes set these values using onwithoptions. can use following instead of onclick suppress further handling of click event after elm code handles it:

onclickfullstop : msg -> html.attribute msg onclickfullstop =   let fullstop = { stoppropagation = true, preventdefault = true }   in onwithoptions "click" fullstop << json.decode.succeed 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -