如果屏幕是特定大小的 php echo 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15142902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:39:43  来源:igfitidea点击:

php echo statement if screen is a certain size

phpjqueryscreen-size

提问by Sam Skirrow

I have a series of jQuery scripts on my site that I want to only run if the users screen width is greater than 960px. I know that you can't detect screen size using php but is there a way to create something to this effect:

我的网站上有一系列 jQuery 脚本,我只想在用户屏幕宽度大于 960 像素时运行。我知道您无法使用 php 检测屏幕大小,但有没有办法创建这种效果:

<? php 
if [METHOD TO DETECT SCREEN SIZE] > 960px {
echo '<script src="js/nbw-parallax.js" type="text/javascript"></script>';
}
?>

采纳答案by Repox

Why not use jQuery?

为什么不使用jQuery?

if( $(window).width() > 960 )
{
  $.ajax({
    url: 'js/nbw-parallax.js',
    dataType: "script",
    success: function() {
        //success
    }
  });
}

回答by Jordi Kroon

PHP is server side and can't grab your screen width and height.

PHP 是服务器端,无法获取您的屏幕宽度和高度。

You have to use javascript.

你必须使用javascript。

JQuery

查询

if( $(window).width() > 960 ) {
     $.getScript('js/nbw-parallax.js');
}

JavaScript

JavaScript

if( window.innerWidth > 960 ) {
    //Your Code
}

回答by davidcondrey

<?php
    if ( stristr($ua, "Mobile" )) {
        $DEVICE_TYPE="MOBILE";
    }

    if (isset($DEVICE_TYPE) and $DEVICE_TYPE=="MOBILE") {
        echo '<script src="js/nbw-parallax.js" type="text/javascript"></script>';
    }
?>

Here's a link to a more detailed script: PHP Mobile Detect

这是更详细脚本的链接: PHP Mobile Detect

回答by Patrick James McDougle

You can have a javascript that sets a cookie containing the user's screen size. (Sure it won't work on the first request, but every subsequent request will work). Then in php you can get the value of the cookie.

您可以使用 javascript 设置包含用户屏幕大小的 cookie。(当然它不会对第一个请求起作用,但每个后续请求都会起作用)。然后在 php 中你可以得到 cookie 的值。

回答by Nick Andriopoulos

you will have to use a javascript (or jQuery) function to generate the new script tags depending on the screen size. You will probably find this http://api.jquery.com/jQuery.getScript/helpful.

您将不得不使用 javascript(或 jQuery)函数根据屏幕大小生成新的脚本标签。您可能会发现这个http://api.jquery.com/jQuery.getScript/很有帮助。

回答by Kirill Kulakov

you should make the JS to detect the resolution and choose by itself whether it should run some code or not.

您应该让 JS 检测分辨率并自行选择是否应该运行某些代码。