【发布时间】:2019-06-10 11:19:49
【问题描述】:
我有一个c#代码,只有在客户端浏览器宽度小于1200px时才需要实现
我试过这段代码:
$(document).ready(function () {
$("#clientScreenWidth").val($(window).width());
$("#clientScreenHeight").val($(window).height());
});
<body>
<input type="hidden" value=""
name="clientScreenHeight" id="clientScreenHeight" />
<input type="hidden" value=""
name="clientScreenWidth" id="clientScreenWidth" />
@{
string height = HttpContext.Current.Request.Params["clientScreenHeight"];
string width = HttpContext.Current.Request.Params["clientScreenWidth"];
if(width <1200)
{
foreach (var component in leftSideComponents)
{
//// Code
}
}
}
</body>
但宽度返回 null。
有人知道如何实现吗?
谢谢
【问题讨论】:
-
您是否有理由决定这应该发生在服务器端而不是客户端?通常CSS media query 在这里是一个合适的解决方案。
-
你知道 JS 和 C# 在不同的时间和不同的计算机上运行吗?
-
Thomas,是的,我知道这一点,但我提出了这个解决方案,因为堆栈溢出一直要求我添加代码
-
@spender 不,这对我的情况没有用,因为我在特定情况下渲染大量图像,我不希望在所有情况下都渲染它们然后隐藏它们通过 CSS
-
您愿意使用 javascript + CSS 解决方案吗?您可以将图像链接传递给 javascript 并根据客户端的屏幕生成 HTML(最初测试宽度并在调整大小时仅生成一次 HTML)宽度并在生成后使用一些 CSS 隐藏或显示它。
标签: javascript c# razor