我试图做一些相当简单的事情,但我不是一个好的javascript。我想做的是:1.通过Javascript获取页面宽度2.如果页面宽度小于400px,则将php变量设置为1
我的JAVASCRIPT 很差
//javascript
var width = $(window).width(); //get the width of the screen
if (width<400) {
$slidevar = 1;
}
然后在页面下方,我可以获得该变量,并使用它来显示我的幻灯片
//PHP
if ($slidevar == 1) {
//Display Slideshow #1 PHP Function
} else {
//Display Slideshow #2 PHP Function
}
所以这是不可能的。
你能在页面加载时一次用PHP检索页面宽度吗?
我找到了这个代码,但我需要怎么做才能获得我的函数?
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
var width = $(window).width();
//This is ajax code which will send width to your php page in the screenWidth variable
$.ajax({
url: "example.php", //this will be your php page
data : {screenwidth:width}
}).done(function() {
$(this).addClass("done");
});
</script>
//example.php code is here :
<?php
echo $width = $_REQUEST['screenwidth'];
?>
jQuery:
var width = $(window).width(); //get the width of the screen
if (width<400) {
var slidevar = 1;
$.ajax({
url:"the/php/page",
data: {'slidevar':slidevar},
type: 'get',
success: function(){
// do something
}
});
}
php:
$slidevar = $_GET['slidevar'];
if ($slidevar == 1) {
//Display Slideshow #1 PHP Function
} else {
//Display Slideshow #2 PHP Function
}
我们可以在这里实现
在javascript中计算屏幕宽度并进行ajax调用以相应地呈现滑块
var width = screen.width;
if (width < 400) {
//AJAX call to render slider1
} else {
//AJAX call to render slider2
}