現在、ネイティブソリューションはありません。 Targeting position:sticky elements that are currently in a 'stuck' stateを参照してください。しかし、私は固有のposition: sticky
と、スティッキーな動作を実装するポリフィルの両方で動作するCoffeeScriptソリューションを持っています。
あなたはスティッキーになりたい要素に「スティッキー」クラスを追加します。
.sticky {
position: -webkit-sticky;
position: -moz-sticky;
position: -ms-sticky;
position: -o-sticky;
position: sticky;
top: 0px;
z-index: 1;
}
CoffeeScriptの「スティッキー」要素の位置を監視し、彼らは「スティッキー」状態にあるとき「スタック」クラスを追加するには:
$ -> new StickyMonitor
class StickyMonitor
SCROLL_ACTION_DELAY: 50
constructor: ->
$(window).scroll @scroll_handler if $('.sticky').length > 0
scroll_handler: =>
@scroll_timer ||= setTimeout(@scroll_handler_throttled, @SCROLL_ACTION_DELAY)
scroll_handler_throttled: =>
@scroll_timer = null
@toggle_stuck_state_for_sticky_elements()
toggle_stuck_state_for_sticky_elements: =>
$('.sticky').each ->
$(this).toggleClass('stuck', this.getBoundingClientRect().top - parseInt($(this).css('top')) <= 1)
注:このコードは、垂直のスティッキー位置でのみ機能します。
その記事のトップクラスのコメントはまさにあなたの問題を解決しますので、それは面白いです。その男はそれにスポットがある、これはメディアのクエリではなく、プロパティでなければなりません。そうすれば、要素が固まったときにスタイルを変えることができます(これはしばしば行います)。ああ、男は夢を見ることができます。 –
ええ、私はそのコメントに気づいた、彼の提案ははるかに良いようです。それでも、「position:sticky」はChromeが実装したものなので、私はそれを使用可能にする方法を探しています! – AlecRust
解決方法を見つけましたか? –