简体   繁体   English

根据变色更改产品图片

[英]Change product image based on variant color

Online example of my goal: 我的目标的在线示例:

I'm trying to display bike products on a category page. 我正在尝试在类别页面上显示自行车产品。 On each product there's a frame variant color, I'm displaying that using round divs. 在每种产品上都有一种框架变色,我正在使用圆形div来显示。

When I click a color, the image should change as you can see on Specialized website: (underneath the product image) Specialized Example . 当我单击一种颜色时,图像应该会发生变化,就像在Specialized网站上看到的那样:(在产品图像下方) Specialized Example

My own website 我自己的网站

You can see my own example here: My own website example (scroll down till you see the product with multiple products). 您可以在此处看到我自己的示例: 我自己的网站示例 (向下滚动直到看到包含多个产品的产品)。

My target functionality: 我的目标功能:

When I click the GREEN div I'd like to show picture 2 and when I click the RED div I'd like to see image 3, but as Default it will always be the firs 当我单击GREEN div时,我想显示图片2;当我单击RED div时,我想看到图片3,但作为Default,它将始终是第一个

  • The first color = the first image 第一种颜色=第一张图像
  • The second color = the second image 第二种颜色=第二种图像
  • And so forth 依此类推

My HTML: 我的HTML:

<div class="frameColors">
    <div class="frameColor" data-color="#95BD40" style="background-color:#95BD40"></div>
    <div class="frameColor" data-color="#000000" style="background-color:#000000"></div>
    <div class="frameColor" data-color="#C6352D" style="background-color:#C6352D"></div>
</div>
<a href="/produkter/cykler/mountain-bikes/specialized/epic-hardtails/epic-hardtail/">
    <div class="categoryImage" data-frame-color="95BD40">
        <img src="/media/1072/165519.jpg?anchor=center&amp;mode=crop&amp;width=500&amp;height=350&amp;rnd=131200748910000000" class="productImage">
    </div>
    <div class="categoryImage" data-frame-color="000000">
        <img src="/media/1071/165518.jpg?anchor=center&amp;mode=crop&amp;width=500&amp;height=350&amp;rnd=131200746750000000" class="productImage">
    </div>
    <div class="categoryImage" data-frame-color="C6352D">
        <img src="/media/1073/166762.jpg?anchor=center&amp;mode=crop&amp;width=500&amp;height=350&amp;rnd=131200749050000000" class="productImage">
    </div>
</a>

My Foreach(s): 我的Foreach:

  var imageIds = item.GetPropertyValue<string>("productImages").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
  var images = Umbraco.Media(imageIds);

     <div class="frameColors">
                @foreach (var bikeColor in images)
                {
                    var color = bikeColor.GetPropertyValue("frameColor");

                    <div class="frameColor" data-color="#@color" style="background-color:#@color"></div>
                }
            </div>

            <a href="@item.Url">
                @foreach (var img in images)
                {        
                    <div class="categoryImage" data-frame-color="@img.GetPropertyValue("frameColor")">
                        <img src="@img.GetCropUrl("product image")" class="productImage" />
                    </div>
                }
            </a>

Based on Adeneo reply I managed to modify the code a little so it worked out as I wanted. 根据Adeneo的回复,我设法对代码进行了一些修改,以便按我的意愿进行工作。

First, I added a css line: .categoryImage { display:none; } 首先,我添加了一个CSS行: .categoryImage { display:none; } .categoryImage { display:none; } to hide ALL the product images. .categoryImage { display:none; }隐藏所有产品图像。

I then added the script based on Adeneo's reply. 然后,我根据Adeneo的回复添加了脚本。 I start off with targeting EACH of the .frameColor divs and inside that, I added this the following line to show the first image: categoryImage.first().show(); 我从定位.frameColor div的EACH开始,并在其中添加以下行以显示第一个图像: categoryImage.first().show(); .frameColor

If the CSS line is not included, all of the product images will be displayed by default, so it was necessary to HIDE all the images and inside the script, show the first image. 如果不包括CSS行,则默认情况下将显示所有产品图像,因此有必要隐藏所有图像,并在脚本内显示第一个图像。

    $(function () {
    $(".frameColor").each(function () {
        var categoryImage = $(this).parent("div").next("a").find(".categoryImage");
        categoryImage.first().show();

        if ($(categoryImage).length > 1) {

            $(this).on('click', function () {
                var color = $(this).data('color').replace('#', '');

                $(categoryImage).hide().filter(function () {
                    return $(this).data('frame-color') === color;
                }).show();
            });
        }
        else {
            $(this).hide();
        }
    });
});

Credits goes to Adeneo for providing the original script. 感谢Adeneo提供原始脚本。

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

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