gpt4 book ai didi

javascript - 使用 jquery 或 css 仅在小屏幕上显示 div

转载 作者:行者123 更新时间:2023-11-28 04:16:37 25 4
gpt4 key购买 nike

我想只在小屏幕上显示这段 html 代码。

<div id="NightDiv" class="w3-center" style="padding-top:20px"><i id="nightMode2" class="fa fa-moon-o fa-lightbulb-o w3-cursor-pointer w3-amazon"  aria-hidden="true"></i>
</div>

这是我正在尝试但没有成功的方法:

$(document).ready(function() {

if ((screen.width>=1024) ) {
$("#NightDiv").hide();
}
else {
$("#NightDiv").show();

}
});

最佳答案

为此您不需要 JavaScript。使用 CSS 媒体查询来执行此操作,

/*Hide for larger screens*/
#NightDiv {
display: none;
}

/*show for small screens */
@media screen and (max-width: 1023px) { /* I've given 1023px but you can change to proper width */
#NightDiv {
display: block;
}
}

关于javascript - 使用 jquery 或 css 仅在小屏幕上显示 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46013050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com