简体   繁体   English

基于javascript的幻灯片不起作用

[英]javascript based slideshow not working

I'm trying to implement a simple javascript/jquery-1.6.2 slideshow (I'm not used to javascript), the problem I have is that the items are all shown and I don't know how to hide them. 我正在尝试实现一个简单的javascript / jquery-1.6.2幻灯片(我不习惯javascript),我遇到的问题是所有项目都已显示,并且我不知道如何隐藏它们。

here is my PHP/HTML code: 这是我的PHP / HTML代码:

  <?php
  $c = 0;
  // show parts of latest news:
  foreach ($items AS $data) {

    // news image
    $image = './images/'.$data['image'];
    // description
    $part_description = substr(nl2br($data['content']), 0, 120);
    ?>

    <div id="cap_<?php echo $c; ?>" class="cap" >
      <h3><a href="<?php echo WEBSITE.'/news/view?id='.$data['id']; ?>" >
        <?php echo $data['title']; ?></a></h3>

      <table>
        <tr>
          <td>
            <a href="<?php echo WEBSITE.'/news/view?id='.$data['id']; ?>" >
              <img alt="Item image" width="200"
                   src="<?php echo $image; ?>" />
            </a>
          </td>
          <td>
            <strong>By</strong>
            <?php echo $data['author']; ?>
            <br/>
          </td>
        <tr/>
      </table>

      <p>
        <?php echo stripslashes($part_description); ?>
        <a href="<?php echo WEBSITE.'/news/view?id='.$data['id']; ?>" >
          read more
        </a>
      </p>
    </div>
    <?php
    $c++;
  }
  // show links:
  ?>

  <div class="links">
  <?php
  for ($i=0; $i<$k; $i++) {
    ?>
    <a href="javascript:slideShow(<?php echo $i; ?>, <?php echo $k; ?>);" class="link_number" id="link_<?php echo $i; ?>">
      <?php echo $i+1; ?>
    </a>
    <?php
  }
  ?>

And my js function: 而我的js函数:

function slideShow(id, max) {
  var inputHide = null;
  for (var i=0; i<max; i++) {
    $('#cap_'+id).fadeOut(500);
    inputHide = document.getElementById('cap_'+i);
    document.getElementById('link_'+i).style.backgroundColor = '#999999';
  }
  $('#cap_'+id).fadeIn(500);
  document.getElementById('link_'+id).style.backgroundColor = 'black';
}

I have two questions: 我有两个问题:

Q1: how can I fix this problem and show only one 问题1:如何解决此问题并仅显示一个

Q2: is it better if I use a ready-to-use script like jqueryui? Q2:如果我使用像jqueryui这样的现成脚本会更好吗?

In terms of your second question, It depends on what you need, and your understanding of JS. 关于第二个问题,这取决于您的需求以及您对JS的理解。 If you have a pure JS script, and you understand how and why it works, you can tailor it to your needs more. 如果您有一个纯JS脚本,并且了解它的工作方式和原因,则可以根据自己的需要进行更多调整。 'personalise it', if you like. 如果愿意,可以“个性化”。 However if your knowledge of JS is limited, then a ready to use script may suit you better. 但是,如果您对JS的了解有限,那么可以立即使用的脚本可能会更适合您。

A good way to learn things is to find a pure JS script somewhere (probably many on here), and use that one, but do your research and use your initiative until you understand how it works. 学习事物的一种好方法是在某个地方(这里可能有很多)找到一个纯JS脚本,并使用该脚本,但是请进行研究并利用自己的主动性,直到您了解其工作原理为止。 Then you can use it in other projects, change it, use bits of it. 然后,您可以在其他项目中使用它,对其进行更改,使用它的某些位。 That's how I learnt most of my javascript! 这就是我学习大部分JavaScript的方式!
I've had some trouble with slideshows recently too, good luck! 我最近在幻灯片放映时也遇到了麻烦,祝您好运! :) :)

Q1: this should work: inputHide.style.display = 'none'; Q1:这应该工作: inputHide.style.display = 'none';

Q2: In my opinion, both ways are good 问题2:我认为两种方法都不错

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

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