I've noticed strange auto scroll behaviour when using overflow: hidden;
on a container with focusable elements inside it: https://codepen.io/anon/pen/aVmavx (you can change focus by using tab key).
It does not trigger onScroll()
event since the overflow is set to hidden. So i wonder how I could control / reset this "scroll"?
My goal (it's not included in the demo) is to hide native scroll bar and to re-create a scroll effect with CSS transition on translateY()
when the onFocus()
event is triggered.
Use overflow-x : scroll and overflow-y : hidden , or overflow: scroll hidden instead. Use overflow-x : hidden and overflow-y : scroll , or overflow: hidden scroll instead.
To prevent scrolling with this property, just apply the rule overflow: hidden to the body (for the entire page) or a container element. This hides all content beyond the element's border.
hidden - The overflow is clipped, and the rest of the content will be invisible. scroll - The overflow is clipped, and a scrollbar is added to see the rest of the content. auto - Similar to scroll , but it adds scrollbars only when necessary.
Add CSS. Set the overflow property to “auto”. This value adds scrollbar when the content overflows.
You can do it like this :
When you focusout
the last button, the scroll of the #parent
with overflow-y: hidden
will be reset to 0.
$('#reset').focusout(function(){
$('#parent').scrollTo(0);
});
* {
box-sizing: border-box;
}
html, body {
margin: 0;
background-color: whitesmoke;
font-family: Roboto;
}
main {
margin: 16px auto;
width: 500px;
height: 140px;
background-color: white;
}
#parent {
width: 100%;
height: 100px;
overflow: hidden;
}
ul {
list-style: none;
margin: 0;
padding: 0;
font-size: 0;
counter-reset: line;
}
ul:focus-within {
border: 2px dashed #E91E63;
}
li {
padding: 16px;
width: 50%;
display: inline-block;
text-align: center;
}
button {
border: none;
width: 100%;
display: block;
background-color: #3F51B5;
color: white;
text-decoration: none;
font-size: 16px;
line-height: 1.5;
}
button:hover{
color:red;
}
button:focus {
background-color: #E91E63;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-scrollTo/1.4.2/jquery.scrollTo.min.js"></script>
<div id="parent">
<ul>
<li><button>I am focusable #1</button></li>
<li><button>I am focusable #2</button></li>
<li><button>I am focusable #3</button></li>
<li><button>I am focusable #4</button></li>
<li><button>I am focusable #5</button></li>
<li><button>I am focusable #6</button></li>
<li><button>I am focusable #7</button></li>
<li><button>I am focusable #8</button></li>
<li><button>I am focusable #9</button></li>
<li><button id="reset">Focus out reset the scroll #10</button></li>
</ul>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With