首页 > 解决方案 > 手机和平板电脑检测

问题描述

在滑块显示 4 个视频中,当用户通过移动设备访问时,我需要将视频替换为图片

<div class="hero__player">
    <div class="player">
        <?php

$detect = new Mobile_Detect;
if ($detect->
    isMobile()) {
    $tr = '
        <img src="/templates/assets/images/button_ground_test.jpg" style="width:100%;">
            ';
    $poster = "[xfvalue_videopathimage]";
    if ($poster == "/templates/assets/images/car.jpg") {
        echo $ec;
    } else if ($poster == "/templates/assets/images/health.jpg") {
        echo $tr;
    } else if ($poster == "/templates/assets/images/property.jpg") {
        echo $ec;
    } else if ($poster == "/templates/assets/images/travel.jpg") {
        echo $tr;
    }

} else {
    echo '
            <video class="player__video" height="506" muted="" playsinline="" poster="[xfvalue_videopathimage]" preload="auto" width="506">
                <source src="[xfvalue_videopath]" type="video/mp4">
                    Your browser does not support the video tag.
                </source>
            </video>
            ';

}
?>
        </img>
    </div>
</div>

如何在此代码中包含移动检测?

标签: phpjqueryhtml

解决方案


老实说:你的代码有点乱,到处都是错误。所以,我会重新开始。

首先确保您遵循mobiledetect的安装说明,这样您在使用时就不会出错。

然后尝试运行这段代码:

<?php

// Include and instantiate the class.
require_once('Mobile_Detect.php');
$detect = new Mobile_Detect;

// detect mobiles and tables
if ($detect->isMobile() && $detect->isTablet()) {
   echo "I will be showing images.";
}
else {
   echo "I will be showing videos.";
}

您应该首先让此代码工作。在计算机和移动设备上进行测试。只有在那之后,才需要担心实际显示图像和视频。

如果此代码给您带来任何问题,那么您可以将其放在注释中,最好是您遇到的完整的第一个错误。

我建议您在遇到问题时提出一个关于显示图像和视频的新问题。图片一个问题,视频另一个问题。我这么说的原因是一个问题应该是关于一件事:移动检测、显示图像或显示视频。不是一次全部三个。


推荐阅读