简体   繁体   English

Grails GSP屏幕宽度

[英]Grails GSP Screen Width

I need to check the screen size to change a component. 我需要检查屏幕尺寸以更改组件。 Without knowing the best way, i try use javascript in 不知道最好的方法,我尝试在

 <g:if test="${javascript($(window).width() > 768)}"> //COMPONENT </g:if> <g:else> //OTHER COMPONENT </g:else> 

Anyone can help me saying the best way to resolve this? 任何人都可以帮助我说出解决此问题的最佳方法吗?

Thanks 谢谢

Without knowing that COMPONENT is in your example it's difficult to offer a solid solution. 在您不知道示例中包含COMPONENT情况下,很难提供可靠的解决方案。

However, the major issue you are facing is that you are trying to mix server-side and client-side evaluation which you can't do. 但是,您面临的主要问题是您试图将服务器端和客户端评估混合在一起,而这是您无法做到的。 GSP is evaluated at the server level and doesn't have access to the browser's DOM to get things such as window.width . GSP在服务器级别进行评估,并且无权访问浏览器的DOM来获取诸如window.width

One possible solution is to submit the window.width with the request to the GSP as a request variable so that you can evaluate that using the GSP. 一种可能的解决方案是将带有请求的window.width提交给GSP作为请求变量,以便您可以使用GSP进行评估。

@Joshua is right, you are mixing and matching client and server side code. @Joshua是正确的,您正在混合和匹配客户端和服务器端代码。 You could use a plugin https://github.com/mathifonseca/grails-browser-detection which is a nice wrapper around the httprequest so you can check if you are communicating with a desktop or mobile browser. 您可以使用插件https://github.com/mathifonseca/grails-browser-detection ,该插件是httprequest的一个很好的包装,因此您可以检查是否与台式机或移动浏览器进行通信。 But normally this would be done using stylesheet (@see: http://www.w3schools.com/html/html_responsive.asp ) 但通常可以使用样式表来完成此操作(@see: http : //www.w3schools.com/html/html_sensitive.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM