Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to move divs through an array?

self teaching javascript I wanted to create a menu sort of like Netflix where as you scroll right or left you can go through shows/movies, however, it would be with left and right arrows,
i.e if I moved right, block2 would be in block1 position, block3 in block2, etc

I was thinking that I could assign each block a position in an array:
var menuBar = [block1, block2, block3, block4]; then iterate through the array

for(var i=0; i < menuBar.length(); i++){
switch (e.key) {
            case 'ArrowLeft':
                i--;
                GoThroughMenu();
                break;
            case 'ArrowRight':
                i++;
                GoThroughMenu();
                break;
}

and GoThroughMenu() would move the blocks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

block1.style.left = menuBar[i].style.left + 'px';
block1.style.top = menuBar[i].style.top + 'px';

Right now nothings moving, is there errors in the way I’m going about this?

>Solution :

left and top css properties only work with a non-position: static value (which is the default position value of all elements).

Try giving the menuBar items a position: relative;.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading