サイトを検索して同様の質問がありましたが、解決策が見つかりませんでした。私はそれにスクロールするときにdivの画面の上に固定取得しようとしています。しかし、私はエラーを得続ける:エラーを取得する "未定義のプロパティ 'トップ'を読み取ることができません
"(インデックス):59キャッチされない例外TypeError:59:(インデックス)で未定義 のプロパティ 'トップ' を読み込めません"
//store the element
var cache = '.faixatrabalho';
console.log(cache);
//store the initial position of the element
var vTop = $(cache).offset().top - parseFloat($cache.css('margin-top').replace(/auto/, 0));
$(window).scroll(function (event) {
// what the y position of the scroll is
var y = $(this).scrollTop();
// whether that's below the form
if (y >= vTop) {
// if so, ad the fixed class
$(cache).addClass('stuck');
} else {
// otherwise remove it
$cache.removeClass('stuck');
}
});
</script>
.faixatrabalho {
padding-top: 20px;
\t width: 100vw;
position: static;
top: 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="faixatrabalho">
<div id="trabalhoback" data-scroll-speed="3"></div>
\t <div class="titulo">Nosso Trabalho</div>
\t <div class="texto1">
\t \t <p > Exemple text</p>
\t </div>
</div>
Iまだjqueryを学んでいて、これを解決できません。
あなたはDOMの準備が整う前に、DOM要素にアクセスしようとしています:jQueryのでは、デフォルトでは、ドキュメントの
ready
イベントにハンドラとして設定jQueryの機能自体、内のコードをラップすることによって、これを達成することができますか?ここで作業する文脈はあまりありません。 –コードに2番目のメモである[.ready()](https://learn.jquery.com/using-jquery-core/document-ready/)を見てください: 'var cache = '.faixatrabalho'; 'は実際の要素ではなくセレクタを格納します。要素をキャッシュしたい場合、 'var cache = $( '。faixatrabalho');' – empiric
[Uncaught TypeError:undefinedのプロパティ 'top'を読み込めません](http://stackoverflow.com/質問/ 20175094/uncaught-typeerror-can not-read-property-un-undefined) – Christoph