简体   繁体   English

Laravel - 仅当屏幕尺寸 > 768px 时才包含文件

[英]Laravel - include file only if screen size > 768px

I have a tricky situation where I need to move my Google Ads around on Mobile.我有一个棘手的情况,我需要在移动设备上移动我的 Google Ads。 I figured out that the best way to do this is to call them using @include but only if certain screen size conditions are met.我发现最好的方法是使用 @include 调用它们,但前提是满足某些屏幕尺寸条件。

I am thinking that the following would work我认为以下内容会起作用

@if($screensize < 768px)
  @include('partials.my-advert')
@endif

So my question is how do I get the screen size into PHP?所以我的问题是如何将屏幕大小转换为 PHP? I can get the screen size via JS using $(window).width();我可以通过 JS 使用$(window).width();获取屏幕尺寸$(window).width(); but then how do I use this value in the Laravel if statement?但是我如何在 Laravel if 语句中使用这个值呢?

You can't detect the screen size with PHP, but alternatively, you can detect the agents.您无法使用 PHP 检测屏幕大小,但也可以检测代理。 There is a package , which detects the agents easily where it lets you detect whether the request is coming from desktop/tablet/mobile.有一个,它可以轻松检测代理,它可以让您检测请求是否来自桌面/平板电脑/移动设备。

To install it via composer, you can run the command below.要通过 composer 安装它,您可以运行以下命令。

$ composer require jenssegers/agent

Then add dd the service provider to providers key within the file config/app.php .然后将服务提供者 dd 添加到文件config/app.php providers 键。

Jenssegers\Agent\AgentServiceProvider::class

Furthermore, add the Agent alias to the aliases key,此外,将代理别名添加到别名键,

'Agent' => Jenssegers\Agent\Facades\Agent::class

Finally, pass the agent variable from your controller to view.最后,从控制器传递代理变量以查看。

$agent = new Agent();
return view('some.view', compact('agent'));

Then within your view, you can check whether the agent belongs to some mobile phone or not.然后在您的视图中,您可以检查代理是否属于某个手机。

@if($agent->isMobile())
  @include('partials.my-advert')
@endif

There's absolutely no point in trying to do it this way, because this entire approach is antiquated.尝试这样做绝对没有意义,因为整个方法已经过时了。 Sure, you could pass the $(window).width() from the client in javascript back to the server in PHP, but then what happens if the user resizes their window?当然,您可以将$(window).width()从 javascript 中的客户端传递回 PHP 中的服务器,但是如果用户调整窗口大小会发生什么?

This is why, today, what is encouraged, instead, is responsive design .这就是为什么,今天鼓励的是响应式设计 Even Google Adsense has responsive ad units for this.甚至Google Adsense 也为此提供了响应式广告单元

Responsive design doesn't require the back-end to know anything about the client in order to properly render things on the page.响应式设计不需要后端了解客户端的任何信息,以便在页面上正确呈现内容。 Instead, the client makes no assumptions whatsoever about how content is rendered on the client UA, and allows advanced CSS and JS to deal with the rendering directly and responsively.相反,客户端对如何在客户端 UA 上呈现内容不做任何假设,并允许高级 CSS 和 JS 直接和响应式地处理呈现。 Meaning, that your content is never different regardless of the screen size.意思是,无论屏幕大小如何,您的内容都不会有所不同。

Bootstrap is one such front-end framework that makes use of responsive design. Bootstrap就是这样一种使用响应式设计的前端框架。 There's also the Material design , which has many implementations that are also responsive.还有Material design ,它有许多响应式的实现。

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

相关问题 仅当屏幕宽度&gt; 768px时才运行脚本的一部分 - run part of the script only when screen width is >768px 检查屏幕大小并在等于或大于 768 像素时执行某些操作 - Check Screen Size and Do Something if equal or greater than 768px 为什么按按钮只能工作 2 次? 当屏幕缩小到小于 768px 时 - Why does pressing the button only work 2 times? when the screen is narrowed to less than 768px zslider javascript应该只能在768px以下的屏幕分辨率下工作 - zslider javascript should work only in screen resolution below 768px 如何调用外部js文件<script src="path_to_file/file_name.js"> for screen size less than 768px? - How to call an external js file <script src="path_to_file/file_name.js"> for screen size less than 768px? 将屏幕大小调整为 768px 时,导航更改样式 - When resizing screen to 768px the navigation change style 停止窗口调整为768px以下 - Stop window resizing below 768px css function @media 屏幕和(最大宽度:768px)在 jquery 中的任何倡议? - Any iniative to the css function @media screen and (max-width: 768px) in jquery? 仅在窗口宽度超过768像素时设置画布高度,否则将其设为默认值 - Only set canvas height when window width is over 768px, otherwise let it default 响应式jQuery不起作用:在768px宽度以下单击,并将鼠标悬停在768px以上 - Responsive jQuery doesn't work: Click below 768px width and hover above 768px
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM