Skip to content

Instantly share code, notes, and snippets.

@Loiree
Created October 18, 2015 18:30
Show Gist options
  • Save Loiree/13ede90afb8af1d1ab19 to your computer and use it in GitHub Desktop.
Save Loiree/13ede90afb8af1d1ab19 to your computer and use it in GitHub Desktop.
Fix
# 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 [0...@fixElems.length]
if window.pageYOffset >= @fixYPos[i]
@fixElems[i].classList.add("fixed")
else
@fixElems[i].classList.remove("fixed")
<div class="fix">
<p>Some text for top menu</p>
</div>
// 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