\r\n\r\n\r\n```\r\n\r\n### 該当のソースコード\r\n\r\n```ここに言語名を入力\r\n効く例\r\n\r\n\r\n\r\n\r\n```","answerCount":2,"upvoteCount":0,"datePublished":"2021-10-25T02:11:14.745Z","dateModified":"2021-10-25T02:11:14.745Z","acceptedAnswer":{"@type":"Answer","text":"事前にautoの時の高さを取得しておく。\r\n\r\n```js\r\nlet h;\r\nwindow.onload=function(){\r\n  div=document.createElement(\"div\");\r\n  div.id=\"a\";\r\n  div.style.width=\"100px\";\r\n  div.style.background=\"red\";\r\n  div.style.transition = \"all .5s ease-in-out\";\r\n  document.body.appendChild(div);\r\n\r\n  div2=document.createElement(\"div\");\r\n  div2.style.width=\"100px\";\r\n  div2.style.height=\"200px\";\r\n  div2.style.background=\"blue\";\r\n  div2.style.overflow=\"hidden\";\r\n  div.appendChild(div2);\r\n  \r\n  div.style.overflow='auto';\r\n  div.style.height='auto';\r\n  h = div.offsetHeight + 100;  \r\n  div.style.height=\"100px\";\r\n  div.style.overflow=\"hidden\";\r\n}\r\nfunction slidedown(){\r\n  a.style.height='100px';\r\n  a.style.height= h + 'px';\r\n}\r\n```\r\n\r\n```html\r\n\r\n```","dateModified":"2021-10-25T03:34:02.661Z","datePublished":"2021-10-25T03:34:02.661Z","upvoteCount":2,"url":"https://teratail.com/questions/366062#reply-497485"},"suggestedAnswer":[{"@type":"Answer","text":"> `div.style.height=\"100px\";`\r\n\r\nこれを `div.style.maxHeight=\"100px\";` に、\r\n\r\n> `a.style.height='auto'`\r\n\r\nこれを `a.style.maxHeight='100vh'` (`100vh`は `auto` のときの高さを超えることが確実でできるだけ小さい値) にします。\r\n`'100vh'`に向かってアニメーションするので、正確に`auto`のときの高さにアニメーションする場合とは多少タイミングがずれます。厳密なアニメーションが必要なら、JavaScriptで`auto`の高さをなんとか計算しておく必要があります。","dateModified":"2021-10-25T02:57:47.922Z","datePublished":"2021-10-25T02:50:52.136Z","upvoteCount":1,"url":"https://teratail.com/questions/366062#reply-497477","comment":[{"@type":"Comment","text":"\r\n\r\n\r\n\r\n上記のようにしたところ何も動かなくなってしまいました\r\nブラウザFirefox","datePublished":"2021-10-25T03:00:40.186Z","dateModified":"2021-10-25T03:04:32.527Z"},{"@type":"Comment","text":"「何も動かなく」とは具体的にどういう状態でしょう? 画面に青い四角が現れない? 四角は現れるけど、ボタンを押しても何も起きない?","datePublished":"2021-10-25T03:32:59.181Z","dateModified":"2021-10-25T03:32:59.181Z"},{"@type":"Comment","text":"四角は現れるけど、ボタンを押しても何も起きないです","datePublished":"2021-10-25T03:37:31.490Z","dateModified":"2021-10-25T03:37:31.490Z"}]}],"breadcrumb":{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"https://teratail.com","name":"トップ"}},{"@type":"ListItem","position":2,"item":{"@id":"https://teratail.com/tags/JavaScript","name":"JavaScriptに関する質問"}},{"@type":"ListItem","position":3,"item":{"@id":"https://teratail.com/questions/366062","name":"transitionが効きません"}}]}}}
前提・実現したいこと
JavaScriptで設定したtransitionが効きません
原因はわかっています
height="auto"にすると効きません
ですがautoは外せません
class名で定義とかはしたくないです
どうしたらいいでしょうか
該当のソースコード
効かない例
<script>
window.onload=function(){
div=document.createElement("div");
div.id="a";
div.style.width="100px";
div.style.height="100px";
div.style.background="red";
div.style.transition = "all 0.5s ease-in-out";
div.style.overflow="hidden";
document.body.appendChild(div);
div2=document.createElement("div");
div2.style.width="100px";
div2.style.height="200px";
div2.style.background="blue";
div2.style.overflow="hidden";
div.appendChild(div2);
}
</script>
<input type="button" value="a" onclick="a.style.overflow='auto';a.style.height='auto'">
該当のソースコード
効く例
<script>
window.onload=function(){
div=document.createElement("div");
div.id="a";
div.style.width="100px";
div.style.height="100px";
div.style.background="red";
div.style.transition = "all 0.5s ease-in-out";
div.style.overflow="hidden";
document.body.appendChild(div);
div2=document.createElement("div");
div2.style.width="100px";
div2.style.height="200px";
div2.style.background="blue";
div2.style.overflow="hidden";
div.appendChild(div2);
}
</script>
<input type="button" value="a" onclick="a.style.overflow='auto';a.style.height='300px'">
ベストアンサー
事前にautoの時の高さを取得しておく。
js
1let h;
2window.onload=function(){
3  div=document.createElement("div");
4  div.id="a";
5  div.style.width="100px";
6  div.style.background="red";
7  div.style.transition = "all .5s ease-in-out";
8  document.body.appendChild(div);
9
10  div2=document.createElement("div");
11  div2.style.width="100px";
12  div2.style.height="200px";
13  div2.style.background="blue";
14  div2.style.overflow="hidden";
15  div.appendChild(div2);
16  
17  div.style.overflow='auto';
18  div.style.height='auto';
19  h = div.offsetHeight + 100;  
20  div.style.height="100px";
21  div.style.overflow="hidden";
22}
23function slidedown(){
24  a.style.height='100px';
25  a.style.height= h + 'px';
26}
html
1<input type="button" value="a" onclick="slidedown()">
div.style.height="100px";
これを div.style.maxHeight="100px"; に、
a.style.height='auto'
これを a.style.maxHeight='100vh' (100vhは auto のときの高さを超えることが確実でできるだけ小さい値) にします。
'100vh'に向かってアニメーションするので、正確にautoのときの高さにアニメーションする場合とは多少タイミングがずれます。厳密なアニメーションが必要なら、JavaScriptでautoの高さをなんとか計算しておく必要があります。
15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.29%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる
 質問する
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/10/25 03:37