我在网站上有一个滑块。滑块称为任意滑块。
您可以在下面的链接上查看它:
http://css-tricks.com/examples/AnythingSlider/#&panel1-1
滑块允许嵌入youtube视频。
不过,目前我有一个php的外观,是通过一个目录循环显示任何横幅图片,上传到这个目录。
我不确定如何添加一个youtube链接作为其中的一个滑块,但仍然保持循环通过目录。
下面是我使用的代码:
<ul id="slider">
<?php
if ($handle = opendir('public/slider/'))
{
while (false !== ($file = readdir($handle)))
{
$files[] = $file;
}
$files = array_slice($files, 2);
foreach ($files as $file)
{
$name = explode('.', $file);
print '<li><img src="public/slider/'.$file.'" title="'.$name[0].'" /></li>';
}
closedir($handle);
}
?>
有什么建议吗?
谢谢
最佳答案
试试这个:
<?php
//will hold anything that you want displayed
$content = array(
array('type' => 'yt', 'content' => 'http://www.youtube.com/watch?v=nn2FB1P_Mn8'),
array('type' => 'yt', 'content' => 'http://www.youtube.com/watch?v=8mwKq7_JlS8')
);
if ($handle = opendir('public/slider/'))
{
while (false !== ($file = readdir($handle)))
{
$content[] = array('type' => 'img', 'content' => $file);
}
closedir($handle);
}
//$content = array_slice($content, 2);
foreach ($content as $liItem)
{
switch($liItem['type']){
case 'img':
$name = explode('.', $liItem['content']);
print '<li><img src="public/slider/'.$liItem['content'].'" title="'.$name[0].'" /></li>';
break;
case 'yt':
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $liItem['content'], $match)) {
echo '<li><iframe width="420" height="315" src="http://www.youtube.com/embed/'.$match[1].'" frameborder="0" allowfullscreen></iframe></li>';
}
break;
}
}
?>
关于php - 将Youtube链接添加到php循环上的滑块,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12179930/