If there is any solution (instead of animation) to jump, you can get rid of pure CSS without javascript at all.
The direction of HTML selection cannot be controlled like in every browser, but you can use the radio buttons and manage the corresponding shortcuts. Then it's just a matter of controlling the display of labels on hover.
html{ font-family:sans-serif; box-sizing:border-box; } input{ display:none; } label{ display:none; padding:1em; width: 300px; } input:checked + label{ background:green; display:block; } #container{ border:1px solid lightblue; position:absolute; top:50%; left:50%; transform:translate(-50%); } #container:hover{ transform:translate(-50%, calc(-50% + 2em)); } #container:hover label{ cursor: pointer; display:block; border-bottom:1px solid lightblue; }
<div id="container"> <input type="radio" name="result" id="f2"> <label for="f2"> France 2</label> <input type="radio" name="result" id="f1"> <label for="f1"> France 1</label> <input type="radio" name="result" id="0" checked> <label for="0"> Draw</label> <input type="radio" name="result" id="i1"> <label for="i1"> Ireland 1</label> <input type="radio" name="result" id="i2"> <label for="i2"> Ireland 2</label> </div>
Edit:
Apparently, in my insanity βyou can do it with pure CSSβ, I forgot about it in 2018, and the above answer will only work on the desktop and fail on mobile devices, as it relies on: hover. So we need a bit of JS just to switch the βactiveβ class instead of hovering.
var container = document.getElementById("container"); var labels = document.querySelectorAll("#container label") for (i = 0; i < labels.length; i++) { (function(i) { labels[i].addEventListener('click', function() { container.classList.toggle("active"); }); })(i); }
html{ font-family:sans-serif; box-sizing:border-box; } input{ display:none; } label{ display:none; padding:1em; width: 300px; } input:checked + label{ display:block; background-color:green; color:white; } #container{ border:1px solid lightblue; position:absolute; top:50%; left:50%; transform:translate(-50%); } #container.active{ transform:translate(-50%, calc(-50% + 2em)); } #container.active label{ display:block; border-bottom:1px solid lightblue; } #container label{ cursor: pointer; }
<div id="container"> <input type="radio" name="result" id="f2"> <label for="f2"> France 2</label> <input type="radio" name="result" id="f1"> <label for="f1"> France 1</label> <input type="radio" name="result" id="0" checked> <label for="0"> Draw</label> <input type="radio" name="result" id="i1"> <label for="i1"> Ireland 1</label> <input type="radio" name="result" id="i2"> <label for="i2"> Ireland 2</label> </div>
You have some problems trying to apply the click event to the container instead of each individual label so that JS can certainly be improved ... any help would be appreciated.
Alternatively, you can simply use jQuery for this. Talkin 'bout "I forgot about this 2018" ...
$('#container label').click(function() { $(this).parent().toggleClass('active') });
source share