简体   繁体   English

创建基本的可滚动缩略图滑块而不刷新页面

[英]Creating a basic scrollable thumbnail slider without page refresh

Relative JavaScript newbie here looking for some help in creating a scrollable thumbnail slider using JavaScript/AJAX/HTML. 相对JavaScript新手在这里寻找一些使用JavaScript / AJAX / HTML创建可滚动缩略图滑块的帮助。

By 'scrollable thumbnail slider' I mean a set of thumbnail images wedged between 'left' and 'right' arrow images. “可滚动的缩略图滑块”是指在“左”和“右”箭头图像之间楔入的一组缩略图图像。 Clicking either arrow should alter the set of thumbnails displayed WITHOUT reloading/refreshing the rest of the page. 单击任一箭头都应该改变显示的缩略图集,而不会重新加载/刷新页面的其余部分。 As an example please see this page: http://www.travelzoo.com/local-deals/Boston/Entertainment/34729 . 例如,请参阅此页: http//www.travelzoo.com/local-deals/Boston/Entertainment/34729

Here's my code thus far: 到目前为止,这是我的代码:

<div id="thumbnails">
  <a href="#" onclick="slideLeft()"><img src="left_arrow.jpg"/></a>
  <script>
    //I only want to display 4 thumbnails at a time, but may have many more cached
    for (var i=0;i<4;i++)
    {
       // don't worry about the formula below, it simply manipulates the imgCounter var
      document.write('<a href=""><img src="sampleImage'+(((parseInt(imgCounter)+i-1)%4)+1)+'.jpg"/></a>');
    } 
  </script>
  <a href="#" onclick="slideRight()"><img src="right_arrow.jpg"/></a>
</div>

<script>
  ...

  function slideLeft() {
    //imgCounter is a passed var, updated when either arrow is clicked
    imgCounter = (parseInt(imgCounter)+3)%4;
    if (imgCounter == 0)
      imgCounter = 4;
    }
  ...
</script>

When the user clicks the left or right arrow, the variable representing the first image (imgCounter) is updated. 当用户单击向左或向右箭头时,将更新表示第一个图像(imgCounter)的变量。 I then want to redraw the DIV (id="thumbnails") with this updated variable while leaving the rest of the page AS IS. 然后我想用这个更新的变量重绘DIV(id =“缩略图”),同时保留页面的其余部分。

Can this be done with pure JavaScript? 这可以用纯JavaScript完成吗?
If not, can someone please provide a basic AJAX example? 如果没有,有人可以提供基本的AJAX示例吗?

If you feel that this question has already been answered (I checked) or is not adequately explained, please don't be a jerk/troll and politely suggest what additional info I can provide. 如果你觉得这个问题已经得到解答(我已经检查过)或者没有得到充分的解释,请不要成为一个混蛋/巨魔并且礼貌地建议我可以提供哪些其他信息。

Thanks! 谢谢!

Ok, so I figured this out myself. 好的,所以我自己想出来了。 As I suspected, I was over-thinking it... 我怀疑,我过度思考了......

I wrote a helper function that generates the innerHTML code for my div, as follows: 我编写了一个帮助函数,为我的div生成innerHTML代码,如下所示:

function generateThumbHTML() {
  var html = '';

  for (var i=0;i<4;i++)
  {
    html += '<a href=""><img src="sampleImage';
    html += '(((parseInt(imgCounter)+i-1)%4)+1)';
    html += '.jpg"/></a>';
  }

  return html;
}

Then, I added a call to my slideLeft() and slideRight() functions, setting the innerHTML property of my DIV to the new HTML code: 然后,我添加了对我的slideLeft()和slideRight()函数的调用,将我的DIV的innerHTML属性设置为新的HTML代码:

<script>
  ...

  function slideLeft() {
    //imgCounter is a passed var, updated when either arrow is clicked
    imgCounter = (parseInt(imgCounter)+3)%4;
    if (imgCounter == 0)
      imgCounter = 4;

    var container = document.getElementById("thumbnails");
    container.innerHTML = generateThumbHTML();
  }
  ...
</script>

The only remaining question I'm trying to answer is this... 我试图回答的唯一问题是......

Why does updating the innerHTML property automatically kick in? 为什么更新innerHTML属性会自动启动? Is there an event that takes place in the background as soon as this property is updated? 此属性更新后是否会在后台发生事件? And if so, how can I find out what events are being fired? 如果是这样,我怎么能找出被解雇的事件? I'm looking at the HTML DOM spec but am not finding the innerHTML property on any object definition. 我正在查看HTML DOM规范,但我没有在任何对象定义上找到innerHTML属性。

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

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