JSの前にCSS読み込まないと色々うまくいかないことがある
head内にlinkでcss読み込んで、scriptでjs読み込んで、中で DOMContentLoaded だの jQuery(function(){}) だのやって云々とかやりますよね。これ、
- link要素でCSS読み込み
- script要素でJavaScript読み込み
の順番でやらないと上手くいかないことがあるみたいです。って今日はじめて知りました。
NGの方を何度もリロードすると、ピンクのdivがたまにslideDownしない。自分がこれを確認したのはChrome。ver.16.0.912.63。理由は、link要素を先に書かないと、DOMContentLoad時に外部スタイルシートが適用されてないかららしいです。スタイルシートが適用されてないと要素の高さが0になっちゃっていてアニメーションできなくなってるっぽい。これは、こんなサンプルにたいに、めちゃめちゃシンプルな場合にしか発生しなさそうだけど、そんなことがあるのか…。以下のサイトにそれっぽいことが詳しく書いてありました。パラレルで読み込むとかDOMContentLoadedのタイミングがどうとかがなんかめんどいらしい。
jQueryの.ready()のドキュメントにも、以下のような記述があった。
When using scripts that rely on the value of CSS style properties, it’s important to reference external stylesheets or embed style elements before referencing the scripts.ということで、CSS読み込みは先に!ってことみたいです。
いつも無意識でそうしてたけどしらなんだ。っていうか結構みんな知らないと思うたぶん…。
javascript css
|
46 notes (#)
|
Comments
|
11/12/15 Thu 19:50:49
($)
|
author:syoichi
e-mail:tsuyuhara-syoichi@hotmail.co.jp
author:syoichi
e-mail:tsuyuhara-syoichi@hotmail.co.jp
-
kellia-springer reblogged this from hamalog
-
getovertheborder reblogged this from hamalog
-
kazzxz reblogged this from syoichi
-
duf20 liked this
-
orihika reblogged this from dotnuke
-
petaweb liked this
-
kgroope reblogged this from syoichi
-
wevnote reblogged this from hamalog
-
hidkick reblogged this from syoichi
-
mercure reblogged this from sai10t and added:
head内にlinkでcss読み込んで、scriptでjs読み込んで、中で DOMContentLoaded だの jQuery(function(){}) だのやって云々とかやりますよね。これ、 link要素でCSS読み込み...
-
sisy liked this
-
shotasakamoto reblogged this from syoichi
-
maechuu reblogged this from hamalog
-
roughtab reblogged this from syoichi
-
cuon reblogged this from sai10t
-
yosha liked this
-
sai10t reblogged this from syoichi
-
sai10t liked this
-
tkyis reblogged this from syoichi
-
ken0205 reblogged this from syoichi
-
nayutanized reblogged this from soramugi
-
dotnuke reblogged this from syoichi
-
saruge reblogged this from syoichi
-
lugecy reblogged this from syoichi
-
athsear reblogged this from syoichi
-
soramugi reblogged this from syoichi
-
atm09td reblogged this from fingaholic
-
castellxxx reblogged this from syoichi
-
kidsl liked this
-
garyuten reblogged this from hamalog
-
stdesign reblogged this from syoichi
-
asanons liked this
-
asanons reblogged this from syoichi
-
mitakusam337 reblogged this from syoichi and added:
head内にlinkでcss読み込んで、scriptでjs読み込んで、中で DOMContentLoaded だの jQuery(function(){}) だのやって云々とかやりますよね。これ、 link要素でCSS読み込み...
-
cr96 reblogged this from syoichi
-
yameroyo liked this
-
yameroyo reblogged this from syoichi
-
fingaholic reblogged this from hamalog
-
ukar reblogged this from syoichi
-
syoichi reblogged this from hamalog
-
hamalog posted this
