单击父图像时不显示图像

时间:2016-03-29 04:10:08

标签: javascript jquery sorting random onclick

功能性:

当用户进入第一页时,主图像被随机化并显示。因此,当用户点击主图像(第1张图像)时,将显示次要图像(第2张图像)。

次要图像是第1张图像的扩展名,因此,例如,当用户点击苹果的第1张图像时,会显示第2张图像。

我做了什么:

我已将第1张图像和第2张图像都设置为一组数组。其次,第一个图像的阵列是随机的,因此,我已将第二个图像阵列设置为附加到第一个随机图像。

我已附上以下代码供您阅读:

//Brand Array
var BrandNameArray = ["http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all"];
//Corresponding Image Array
var OfferArray = ["http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all", "http://loremflickr.com/g/320/240/paris,girl/all"];

$(function() {

  //Auto populate into brand container once randomised for each Brand image
  var BrandNameArrayBackup = JSON.parse(JSON.stringify(BrandNameArray));
  for (i = 0; i < $('#list').find('img').length; i++) {
    //To Set random Brand
    var random_BrandIndex = Math.floor(Math.random() * BrandNameArray.length);
    //Assign Variable to generate random Brands
    var Brand = BrandNameArray[random_BrandIndex];
    BrandNameArray.splice(random_BrandIndex, 1);
    $('#Brand_' + (i + 1)).attr('src', Brand);
    $('#Brand_' + (i + 1)).show();
    console.log(Brand);
  }
  BrandNameArray = BrandNameArrayBackup; //re-assigning values back to array
});

//Choose Brand with popUp
function selectBrand(index) {

  $('#BrandDescription').fadeIn({
    duration: slideDuration,
    queue: false
  });
  $("#All").hide();
  $("#Back").hide();
  $("#Beauty").hide();
  $("#Choose").hide();
  $("#Fashion").hide();

  var chosenBrandIndex = OfferArray[BrandNameArray];
  //Set option clicked to CSS change 
  $('#Description').attr('src', chosenBrandIndex);
  $('#Description').show();
}

function BrandDescription() {
  idleTime = 0;

  $("#border_page").fadeOut(function() {
    $("#BrandDescription").fadeIn();
  });
}
.menu {
  background-color: #FFFFFF;
  filter: alpha(opacity=90);
  opacity: 0.98;
}
.Container {
  position: absolute;
  top: 300px;
  left: 300px;
  height: 600px;
  width: 1260px;
  overflow-y: scroll;
}
.innerScroll {
  position: relative;
  width: 1250px;
  height: 600px;
  font-size: 25px;
  color: #8d8989 !important;
  overflow: scroll;
}
<div id="ChooseBrand" align="center" style="position:absolute; width:1920px; height:1080px; background-repeat: no-repeat; z-index=3; top:0px; left:0px;">
  <img id="Main" src="lib/img/PAGE03/Background.png" />
  <input type="text" id="SearchField" style="position:absolute; top:190px; left:660px; height:40px; width:600px; outline=0px; border: 0; font-size:25px; font-family:'CenturyGothic'; background: transparent; z-index=4;" autofocus src="lib/img/transparent.png">
  <div class="Container">
    <div id="list" class="innerScroll">
      <!--1st Row-->
      <img id="Brand_1" style="width:284px; height:140px; top:0px; left:0px; border:0px; outline:0px" onclick="selectBrand('1');">
      <img id="Brand_2" style="width:284px; height:140px; top:0px; left:330px; border:0px;" onclick="selectBrand('2');">
      <img id="Brand_3" style="width:284px; height:140px; top:0px; left:650px; border:0px;" onclick="selectBrand('3');">
      <img id="Brand_4" style="width:284px; height:140px; top:0px; left:965px; border:0px;" onclick="selectBrand('4');">

      <!--2nd Row-->
      <img id="Brand_5" style="width:284px; height:140px; top:140px; left:0px; border:0px;" onclick="selectBrand('5');">
      <img id="Brand_6" style="width:284px; height:140px; top:140px; left:330px; border:0px;" onclick="selectBrand('6');">
      <img id="Brand_7" style="width:284px; height:140px; top:140px; left:650px; border:0px;" onclick="selectBrand('7');">
      <img id="Brand_8" style="width:284px; height:140px; top:140px; left:965px; border:0px;" onclick="selectBrand('8');">

      <!--3rd Row-->
      <img id="Brand_9" style="width:284px; height:140px; top:280px; left:0px; border:0px;" onclick="selectBrand('9');">
      <img id="Brand_10" style="width:284px; height:140px; top:280px; left:330px; border:0px;" onclick="selectBrand('10');">
      <img id="Brand_11" style="width:284px; height:140px; top:280px; left:650px; border:0px;" onclick="selectBrand('11');">
      <img id="Brand_12" style="width:284px; height:140px; top:280px; left:965px; border:0px;" onclick="selectBrand('12');">
    </div>
  </div>
</div>

<div id="BrandDescription" class="menu" align="center" style="position:absolute; width:1920px; height:1080px; background-repeat: no-repeat; display:none; top:0px; left:0px; z-index=4;">

  <img id="Description" style="position:absolute; top:124px; left:534px;z-index=5;">

</div>

问题:

目前我正面临的主要问题是

**当我点击主图像时,根本没有显示次要图像。因此,没有二次阵列图像被附加到第一随机化图像。因此,不显示次要图像。

我迷失在可能出错或失踪的地方。 请帮忙。

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

看起来你在这里遇到了很多问题,一个是使用内联点击处理程序,这只是不好的做法,另一个是你使用不同的函数作用域,并且在窗口范围内不按顺序声明函数。话虽如此,这里有一些有用的东西:

不要将你的javascript内联绑定,请尝试以下方法:

在您的dom元素上使用数据标记,例如data-brand="1"

<img id="Brand_1" data-brand="1" style="width:284px; height:140px; top:0px; left:0px; border:0px; outline:0px">

将jQuery绑定到整个列表中:

$('#list img').on('click', function(){
    var brandId = $(this).data('brand'));
    selectBrand(brandId);
})

同时将您的函数定义移到脚本块的顶部,这样它们就可以在“准备好”之前定义。函数,因为您在窗口命名空间中将它们声明为全局变量。将selectBrand定义为window.selectBrand,并将其作为window.selectBrand调用也可以。

随后我遇到了一个&#39; slideDuration不是一个功能&#39;错误,但这超出了这个问题的范围。

这是我为解决这个问题所做的jsFiddle:https://jsfiddle.net/TimOgilvy/n1L9ja7m/

答案 1 :(得分:0)

我可以在这里找出代码中的一些问题

  1. 您使用的是$('#Vivo_BrandDescription'),但Vivo_BrandDiscription无效ID,我认为应该是BrandDiscription
  2. 在以下代码中的函数selectBrand(index)

    $('#BrandDescription').fadeIn({
       duration: slideDuration, // <= this here slideDuration is undefined
       queue: false
    });
    
  3. 图片标签中的
  4. 内联onclick="selectBrand('3');",我不知道为什么但它不适合我,所以我做的是

    $('#list').find('img').on('click', function(){
       return selectBrand(this.id.split('_')[1]);
    });
    
  5. 在此处进行这些更改后,

    fiddle

    我已经更新了小提琴以正确获取图像可能这就是你想要的。