+ 2

JavaScript how to keep it moving?

On this code I made, there are left and right buttons. The buttons are supposed to move the cards to the left or to the right. But it only moves them once. (I used transform translate (). I was wondering how I would keep it moving every time the right/left buttons are clicked. And also set a limit to where it can't be moved after it reaches certain distance or after so many clicks. If you don't understand the question please let me know. https://code.sololearn.com/W87bA267g1FA/?ref=app

25th May 2019, 5:37 AM
Ginfio
Ginfio - avatar
27 odpowiedzi
+ 1
remove your previous css completely, we start from scrap now step 2 .container { border:2px solid purple; position:relative; transition:left 500ms; height:350px; display:flex; width:750px; } .card { border:1px solid pink; width:250px; height:350px; }
25th May 2019, 8:41 AM
Gordon
Gordon - avatar
+ 3
The translate doesn't transition them slowly, but instantly. For this kind of animation, I recommend using JS, with intervals, so you can stop it anywhere you want
25th May 2019, 6:20 AM
Airree
Airree - avatar
+ 3
I don't want to step on Gordon 's toes, but if you want to set a limit on how far it can go, you should use JS
25th May 2019, 8:36 AM
Airree
Airree - avatar
25th May 2019, 9:02 AM
Airree
Airree - avatar
+ 3
Airree 's pure JS way is easier to understand. amd more fit for your question. I think it is better for you to mark his answer as best. My way is combination of CSS and JS, I'll find some readings for you.
25th May 2019, 10:38 AM
Gordon
Gordon - avatar
+ 2
<div class="card" > not <div id="card" > setting same id for more than one element will cause problem
25th May 2019, 8:35 AM
Gordon
Gordon - avatar
+ 2
now in css .container { /* for your current html, use #water instead */ border : 2px solid red; } .card { border : 1px solid blue; } these borders are for studying the element position and size, they are temporary and we will remove them at last
25th May 2019, 8:38 AM
Gordon
Gordon - avatar
+ 2
your previous margin-top margin-left float remove all these. the size of body we don't need too.
25th May 2019, 8:42 AM
Gordon
Gordon - avatar
+ 2
https://code.sololearn.com/W94TZ209D5CS/?ref=app sorry i got something to do in life. previous step 3 not working because left is not initially set as 0. left is just the same as right. set left of container as multiple card width in px. transition of css property make it an animation.
25th May 2019, 10:29 AM
Gordon
Gordon - avatar
+ 2
You can begin by reading this lesson about Transition in w3school https://www.w3schools.com/css/css3_transitions.asp
25th May 2019, 12:12 PM
Gordon
Gordon - avatar
+ 2
Ginfio yes it'll be easier to start again from blank code. Firstly, build the HTML structure. One parent to contain the cards. Use class names for elements with same behaviour. Then, the CSS. The parent has position absolute and left 0 The parent has transition : left 1500ms The child has width and height. Then, the JS. set the left of the container as multiple of width of child. You can use non-ES6 syntax, it works too. My functions actually do the same thing as your original functions, the reason there is animation instead of jumping instantly is the CSS property transition.
25th May 2019, 5:01 PM
Gordon
Gordon - avatar
+ 1
it's too early to bother about best answer or not yet
25th May 2019, 8:44 AM
Gordon
Gordon - avatar
+ 1
no float use flex on parent instead https://code.sololearn.com/WxDA4Rvt8wlJ/?ref=app
25th May 2019, 8:53 AM
Gordon
Gordon - avatar
+ 1
and here is step 3 for the js https://code.sololearn.com/W8hN6fY28VUx/?ref=app you can try to complete it by adding function left
25th May 2019, 8:58 AM
Gordon
Gordon - avatar
0
use transition of css you need to redo the html, the css and the js follow my steps one by one: Step 1. in html div class container div class card div class card div class card do it and show the code here, then we do step 2
25th May 2019, 8:29 AM
Gordon
Gordon - avatar
0
ok, let me do that real quick.
25th May 2019, 8:30 AM
Ginfio
Ginfio - avatar
0
for the container, id water ok but for the child, dont id, use class
25th May 2019, 8:34 AM
Gordon
Gordon - avatar
0
Step 1 ✔️ Done (id = class.?)
25th May 2019, 8:34 AM
Ginfio
Ginfio - avatar
0
ok
25th May 2019, 8:35 AM
Ginfio
Ginfio - avatar
0
Gordon, set up good?
25th May 2019, 8:37 AM
Ginfio
Ginfio - avatar