width: 300px;
height: 150px;
width: 300px;
height: 150px;
HTMLとCSSは以下の通りです。最低限の記述です。
<div class="content">
<div class="box"></div>
</div>
CSS
.box {
width: 300px;
height: 150px;
background: red;
color: #fff;
}
const box = document.querySelectorAll(".box");
box.style.width = '90%';
box.style.height = '100px';
box.style.background = 'bule';
ハイフン(-)があるプロパティを記述する時は注意してください。 backgroundImageのようにキャメルケースで記述してください。
box.style.backgroundColor = ''; /*background-color*/
box.style.backgroundImage = ''; /*background-image*/
box.style.fontSize = ''; /*font-size*/
box.style.fontWeight = ''; /*font-weight*/
要素.style.cssText = 'CSSを記述'
const box = document.querySelectorAll(".box");
box.style.cssText = 'width: 90%; height: 100px; background: bule;';
要素.style.プロパティ名 = '(空)'
box.style.width = '';
box.style.height = '';
box.style.background = '';
box.style.cssText = '';
width: 300px;
height: 150px;
const box = document.querySelector(".box");
const changeCss = document.querySelector(".changeCss"); // ボタンを取得
changeCss.addEventListener("click", ()=>{
if(changeCss.textContent == '追加'){
box.style.width = '90%';
box.style.height = '100px';
box.style.background = 'blue';
changeCss.textContent = '元に戻す';
} else {
box.style.width = '';
box.style.height = '';
box.style.background = '';
changeCss.textContent = '追加';
}
})
追加・変更 : 要素.style.プロパティ名 = 値(value);
まとめて記述:要素.style.cssText = 'CSSを記述';
削除 : 要素.style.プロパティ名 = '(空)';
電卓のソースコードを2つ紹介します。javascriptはわずか数行で書けます。
たった4行のJavaScriptで作るハンバーガーメニューの作り方について紹介します。✖をつくるには、transform-originがポイントです。
jqueryのripples-min.jsを使うと簡単に水面の波紋を表現できます。波紋の広がりの速度、波紋の大きさ、波紋のブレの値を変えていろいろ表現させると面白いです。
JavascriptのquerySelectorAllで要素を取得し、クリックしたら、クラスを追加したり、削除したりする方法について解説します。querySelectorと同じように書いてしまうとエラーがでます。forEachを使うとすべてに適用できます。
要素の高さを取得する方法について解説します。getBoundingClientRect、window.innerHeight、window.pageYOffsetについて理解し、要素がいつ画面に現れるかを把握できるようにしましょう。
トップページの記事が増えてきたので、何とかページネーションで最新の記事だけを表示できないかと思っていたら、いい記事を発見しました。カスタマイズして実装することができました。
マウスの指す(X, Y)座標とドラッグ & ドロップについて解説します。mousedown, mouseover, mousemove, mouseupを使います。
javascript(Object.entries)を使って、連想配列の中身をテーブルできれいに表示する方法について紹介します。