0

how to use css Calc function in JavaScript ?

i have a position set by calc function exp position:absolute; top:calc(85vh - 20px); and i need to use that position in javascript but it doesnt work . anyone can help me with it ?

20th Oct 2019, 4:08 PM
Ouardi Yacine
Ouardi Yacine - avatar
9 odpowiedzi
+ 2
idk.. if its possible, but my solution is get the elements by its class. then use style.top = calc() for(let e of document.getElementsByClassName=('yourclass')) e.style.top = calc("85vh - 20px"); but this method doesnt change the css file, instead its adding an inline style in the html tag/elements
20th Oct 2019, 4:18 PM
Taste
Taste - avatar
+ 1
By original position, I think you mean position relative with top 0
20th Oct 2019, 4:35 PM
Gordon
Gordon - avatar
+ 1
ooooh create 2 css rule .normal{ /*original css*/ } .upabit{ top: 60px; } now in jquery you'll only need to toggle class upabit from the element.
20th Oct 2019, 4:50 PM
Taste
Taste - avatar
0
thanks and instead of calc how can i declare original position ? i tried auto and none but it didn't work
20th Oct 2019, 4:26 PM
Ouardi Yacine
Ouardi Yacine - avatar
0
the original position is calculated with css like the example above
20th Oct 2019, 4:38 PM
Ouardi Yacine
Ouardi Yacine - avatar
0
calc is a CSS function, in JavaScript, you assign like this: element.style.top = (window.innerHeight*0.85 - 20) + "px";
20th Oct 2019, 4:41 PM
Gordon
Gordon - avatar
0
this is my code so what can i write instead of original position to go back to original ? $(function() { $(".b").click(function() { $(".pp").animate({top: '60px' }, 1000); }); $(".pn").click(function() { $(".pp").animate({ top: ' original position' }, 1000); }); });
20th Oct 2019, 4:47 PM
Ouardi Yacine
Ouardi Yacine - avatar
0
Store the position before the first animation $(function() { var top = $(”.pp”).position().top; $(".b").click(function() { $(".pp").animate({top: '60px' }, 1000); }); $(".pn").click(function() { $(".pp").animate({ top: top}, 1000); }); });
21st Oct 2019, 7:34 AM
Toni Isotalo
Toni Isotalo - avatar
0
thanks guys that was really helpful
21st Oct 2019, 11:12 AM
Ouardi Yacine
Ouardi Yacine - avatar