0

I am a starter ClojureScript developer, making a front-end application, and so far I didn't have to use Javascript, because reagent is more than enough. But now I want to implement some Jquery for an easy scrollAnimation. Jayq is working, but I have problems with using the interop.

$("#button").click(function() {
$('html, body').animate({
    scrollTop: $("#elementtoScrollToID").offset().top
}, 2000);
});

So far I could manage to write the first part, where I get the position of the div I want to scroll to.

(.-top (.offset ($ "#scrollto")))

But the second part is harder then I thought. I tried it like below and some similar solutions, which also didn't work.

#(.animate ($ "html" "body") (clj->js {"scrollTop" (.-top (.offset ($  "#scrollto")))
}) "2000")

Any help is appreciated!

1 Answer 1

1
(-> (js/$ "html, body")
    (.animate (clj->js {:scrollTop (-> (js/$ "#ide")
                                        (.offset)
                                        .-top)}) 
               2000))
Sign up to request clarification or add additional context in comments.

3 Comments

Could you explain briefly what this code does, how it's different from the original code and why it solves the problem?
Thank you very much, your solution is perfect, but I agree with JJJ-s comment, with some explaining next time I wouldn't even have to ask, I would know how and why it's done like that.
It looks like you were just missing accessing jQuery $ properly. js/$ is the correct syntax for accessing global variables in CLJS. As for the rest of the changes the answerer made, they are syntactic sugar and a pretty common pattern for accessing nested properties and methods of javascript objects in CLJS. The thread first operator '->' takes the first argument you pass it and passes it along as the first argument to subsequent function calls. So for example, the return result of (js/$ "html, body") gets passed as the first arg to .animate. Same as (.animate (js/$ "html, body") ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.