在Div之间旋转图像

时间:2016-12-29 11:21:34

标签: jquery

我试图在一些div中旋转一些图像。到目前为止,我已经达到了有限的jquery技能。

<div id='pos-1'>
</div>
<div id='pos-2'>
</div>
<div id='pos-3'>
</div>


var image = new Array ();
  image[0] = "<img src='https://placehold.it/200/09f/fff.png'/></a>";
  image[1] = "<img src='https://placehold.it/200/000/fff.png'/></a>";
  image[2] = "<img src='https://placehold.it/200/22f/000.png'/></a>";
  image[3] = "<img src='https://placehold.it/200/32f/fff.png'/></a>";
  image[4] = "<img src='https://placehold.it/200/42f/fff.png'/></a>";

var link = new Array ();
    link[0] = "<a href='http://www.jquery.com'>";
    link[1] = "<a href='http://www.microsoft.com'>";
    link[2] = "<a href='http://www.yahoo.com'>";
    link[3] = "<a href='http://www.msn.com'>";
    link[4] = "<a href='http://www.stackoverflow.com'>";

var size = image.length;
var x = Math.floor(size*Math.random());
$('#pos-1').append(link[x]+image[x]);
$('#pos-2').append(link[x]+image[x]);
$('#pos-3').append(link[x]+image[x]);

演示:https://jsfiddle.net/y0hga2of/

此处图像与链接相关联,因此一旦用户点击div,他/她就会被带到该URL。例如:image0与link0相关联,image1与link1相关联,依此类推。

但是使用此代码,所有div中都会显示相同的图像。我想要的是

  • image0和link0应显示在pos-1
  • image1 / link1和image2 / link2应该&#39;旋转&#39;在pos-2
  • image3 / link3和image4 / link4应在pos-3
  • 中旋转

目前我还想在pos-1中只显示一个图像。在稍后阶段,我可以在pos-1中旋转两个图像。同样地,我可能只在pos-2中显示一个图像。我如何在代码中适应这种灵活性?

感谢。

2 个答案:

答案 0 :(得分:1)

您对三个附加语句使用相同的随机String text=driver.findelement(By.xpath("//textarea[@placeholder='E‌​nter a description']")).getAttribute("value"); 值,您需要每次重新计算x,如下所示:

x

查看更新后的fiddle

答案 1 :(得分:0)

您可以根据需要尝试:

jsfiddle.net/y0hga2of/5/

$(function(){
var image = new Array ();
  image[0] = "<img src='https://placehold.it/200/09f/fff.png'/></a>";
  image[1] = "<img src='https://placehold.it/200/000/fff.png'/></a>";
  image[2] = "<img src='https://placehold.it/200/22f/000.png'/></a>";
  image[3] = "<img src='https://placehold.it/200/32f/fff.png'/></a>";
  image[4] = "<img src='https://placehold.it/200/42f/fff.png'/></a>";

var link = new Array ();
    link[0] = "<a href='http://www.jquery.com'>";
    link[1] = "<a href='http://www.microsoft.com'>";
    link[2] = "<a href='http://www.yahoo.com'>";
    link[3] = "<a href='http://www.msn.com'>";
    link[4] = "<a href='http://www.stackoverflow.com'>";

  var min = 0;
  var max = 1;
  var x = Math.floor(Math.random() * (max - min) + min);
  $('#pos-1').append(link[x]+image[x]);

  min=1;
  max=3;
  x = Math.floor(Math.random() * (max - min) + min);
  $('#pos-2').append(link[x]+image[x]);

  min=3;
  max=5;
  x = Math.floor(Math.random() * (max - min) + min);
  $('#pos-3').append(link[x]+image[x]);
});