我正在尝试制作一个页面,当调整窗口小于767时,比刷新页面一次。
所以我搜索了很多例子,这是我的代码
$(document).ready(function(){
$(window).on('resize',function(){
if ($(window).width() < 767) {
location.reload(); // refresh page
}
else {
// width more than 768px for PC
}
});
});
问题是代码在PC浏览器上运行良好。 但是,在移动浏览器上,它可以无限刷新和刷新......
在移动设备时,我需要修改代码以不刷新页面? 仅在PC上窗口大小小于767px时刷新?
答案 0 :(得分:1)
您可以为该
制作会话Cookie$(document).ready(function(){
$(window).on('resize',function(){
if ($(window).width() < 767 && !document.cookie.match('refresh')) {
document.cookie = "refresh=1; expires=0; path=/"
location.reload(); // refresh page
}
else {
// width more than 768px for PC
}
});
});
检查刷新cookie不存在然后刷新页面
答案 1 :(得分:0)
你可以做的是在php中设置一个会话以记住用户是否已经完成了调整大小
答案 2 :(得分:0)
<script>
$(document).ready(function()
{
$width = $('#content').width();
$('#content img').css(
{
'max-width': $width, 'height': 'auto'
});
});
</script>