简体   繁体   English

Javascript / jQuery文本幻灯片

[英]Javascript/jQuery text slideshow

I have been trying to achieve a JavaScript/jQuery text slideshow, but I can't seem to get it to work. 我一直在尝试实现JavaScript / jQuery文本幻灯片演示,但似乎无法使其正常工作。 I have some text coming from 4 rows in my database and I want them to slide. 我的数据库中有4行文本,我希望它们滑动。 I was able to make the text slide by storing my text in an array in my jQuery code, but that isn't what I want. 我可以通过将文本存储在jQuery代码的数组中来使文本幻灯片滑动,但这不是我想要的。 I want the text to come from the database. 我希望文本来自数据库。

PHP/HTML PHP / HTML

<div id="test">
  <?php foreach($slideshows as $slideshow): ?>
  <p id="textSlide">
    <?php echo $slideshow["testing"]; ?>
  </p>
  <?php endforeach; ?>`
</div>

JQuery JQuery的

var textSlider = [
  "Lorem Ipsum is simply dummy text.",
  "Ipsum is simply dummy text.",
  "Lorem Ipsum is simply dummy text.",
  "Ipsum is simply dummy text.",
  "Lorem Ipsum is simply dummy text.",
];

var i = 0;

setInterval(function() {
  $("#textSlide").html(textSlider[i]);
  if (i == textSlider.length) {
    i = 0;
  }else {
    i++;
  }
},2000);

If it works with the array in JavaScript, then a simple solution is to echo the strings from PHP into the array: 如果它与JavaScript中的数组一起使用,那么一个简单的解决方案是将PHP中的字符串回显到数组中:

var textSlider = [
  <?php 
      foreach($slideshows as $slideshow): 
         echo '"'.$slideshow["testing"].'",'; 
      endforeach; 
  ?>
];

You can get the content of p elements and create array with them to use in your slider code. 您可以获取p元素的内容,并使用它们创建数组以在滑块代码中使用。

html/php HTML / PHP

<div id="test">
  <?php foreach($slideshows as $slideshow): ?>
  <p class="textToSlide">
    <?php echo $slideshow["testing"]; ?>
  </p>
  <?php endforeach; ?>`
</div>

js JS

var sliderTexts = [];
$(".textToSlide").each(function() {
    sliderTexts.push($(this).text());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM