Created
October 18, 2015 18:30
-
-
Save Loiree/13ede90afb8af1d1ab19 to your computer and use it in GitHub Desktop.
Fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Fix | |
# фиксирует элементы, когда они на верхней части экрана | |
# IE9+, iOS* (фиксируется только после отпуска пальца) | |
# -------------------------------------------------------- | |
# cache | |
# fixElems — фиксируемые элементы | |
# fixYPos — изначальные y-координаты fixElems | |
# fix — добавляет/удаляет класс "fixed" | |
# -------------------------------------------------------- | |
Fix = do -> | |
init: -> | |
do @cache | |
do @bindEvents | |
do @fix | |
cache: -> | |
@fixElems = document.getElementsByClassName("fix") | |
@fixYPos = [] | |
for i in @fixElems then @fixYPos.push(i.offsetTop) | |
bindEvents: -> | |
addEventListener "scroll", => do @fix | |
fix: -> | |
for i in [[email protected]] | |
if window.pageYOffset >= @fixYPos[i] | |
@fixElems[i].classList.add("fixed") | |
else | |
@fixElems[i].classList.remove("fixed") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="fix"> | |
<p>Some text for top menu</p> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FIX | |
// ================================== | |
.fix | |
background blue | |
border 2px solid lighten(blue, 20%) | |
width 10% | |
padding 30px | |
text-align center | |
position absolute | |
left 50% | |
margin-top 20px | |
transform translateX(-50%) | |
.fixed | |
position fixed | |
top 0 | |
margin-top 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment