Jquery:如何将图像叠加在一起? [英] Jquery: How to layer images on top of one another?

查看:24
本文介绍了Jquery:如何将图像叠加在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我有一个构建衬衫的页面.当用户单击一个选项(男/女")时,#shirt"部分通过将 PNG 图像相互叠加来显示构造的衬衫.例如,选择蓝色"的基色将显示蓝色衬衫的图像.然后,选择罗纹"的身体缝合将在蓝色衬衫的顶部放置一个添加罗纹细节的图像.现在,我的问题是每个新图像都会替换以前的图像.其他一切正常(响应先前选择的选项出现的选项).

选择性别
<input type="radio" name="gender" data-id="" value="male"/><label>Male</label><br><input type="radio" name="gender" data-id="" value="female"/><label>Female</label>

<section id="displaysection"><div id="male" class="desc sex diy-box">Pick Body
<input type="radio" name="male" data-id="105" value=""/><label>Blue</label><br><input type="radio" name="male" data-id="120" value=""/><label>黑色</label><br><input type="radio" name="male" data-id="145" value=""/><label>White</label>

<div id="female" class="desc sex diy-box">Pick Body
<input type="radio" name="female" data-id="107" value=""/><label>Blue</label><br><input type="radio" name="female" data-id="211" value=""/><label>黑色</label><br><input type="radio" name="female" data-id="212" value=""/><label>粉红色</label>

</节><div id="body_stitching" class="diy-box">车身拼接
<input type="radio" name="body_stitching" data-id="" value="body_stitching_plain"/><label>Plain</label><br><input type="radio" name="body_stitching" data-id="" value="body_stitching_rib"/><label>Rib</label>

<section id="displaysection"><div id="body_stitching_plain" class="desc body_stitching diy-box">平原<br><input type="radio" name="body_stitching_plain" data-id="324" value=""/><label>蓝色</label><br><input type="radio" name="body_stitching_plain" data-id="325" value=""/><label>Red</label>

<div id="body_stitching_rib" class="desc body_stitching diy-box">肋骨<br><input type="radio" name="body_stitching_rib" data-id="" value="black"/><label>Black</label><br><input type="radio" name="body_stitching_rib" data-id="" value="green"/><label>Green</label>

</节><div class="clear"></div><div id="衬衫"></div><div id="pricefield" style="float:right"></div><脚本>$(document).ready(function() {$("div.desc").hide();变量数据 = {"105" : { img: "http://oceandrive.localhost/images/diy-images/105.png", label: "color 1", price: "100" },"120" : { img: "http://oceandrive.localhost/images/diy-images/120.png", 标签: "color 2", price: "110" },"145" : { img: "http://oceandrive.localhost/images/diy-images/145.png", 标签: "color 3", price: "120" },"107" : { img: "http://oceandrive.localhost/images/diy-images/107.gif", 标签: "color 4", price: "130" },"211" : { img: "http://oceandrive.localhost/images/diy-images/211.png", 标签: "color 5", price: "140" },"212" : { img: "http://oceandrive.localhost/images/diy-images/212.png", 标签: "color 6", price: "150" },"324" : { img: "http://oceandrive.localhost/images/diy-images/324.png", 标签: "color 7", price: "160" },"325" : { img: "http://oceandrive.localhost/images/diy-images/325.png", label: "color 8", price: "170" },};$('input[name]').click(function() {var 值 = $(this).val();//图片单选按钮的值if(value=='male' || value=='female') {$("div.gender").hide('慢');$("div.gender input:radio").removeAttr('checked');}if(value=='body_stitching_plain' || value=='body_stitching_rib') {$("div.body_stitching").hide('慢');$("div.body_stitching input:radio").removeAttr('checked');}$("#" + value).show('slow');//使用选中的单选按钮值寻址 div如果(this.checked){//var value = $(this).val();var value = $(this).data('id');如果(数据[值] != 未定义){var html = '';html = html + '<img style="z-index:2;"src="'+data[value].img+'"/>';$('#shirt').html(html);var html = '';html = html + '- '+data[value].label+' - '+data[value].price+' NT
';$('#pricefield').html(html);}}});});

解决方案

首先,你需要决定你的图像应该以什么顺序出现.例如,从你的代码中我会说你有两个级别(你可以添加更多级别如果您需要它们):

  1. 基础
  2. 车身缝合

其次,将每个级别分配给一个 z-index 并为每个级别创建

元素.这些

的样式应设置为绝对定位,如下所示:

<div id="baseImage" style="z-index:1; position: absolute;"></div><div id="stitchImage" style="z-index:2; position: absolute;"></div>...

我可能还会指定每个

的宽度/高度以匹配您的图像.

最后,使用 jQuery 将每个

放置在基本图像的顶部.

$("#stitchImage").offset($("#baseImage").offset());

然后,当您需要替换图像时,只需使用您拥有的代码:

$("#stitchImage").html("<img src='" + src + "'/>");

请注意,您必须使用空白的占位图片,以便用户在尚未选择较高层的情况下可以看到较低的层.

In the following code I have a page that constructs a shirt. As the user clicks an option ("Male/Female"), the '#shirt' section displays the constructed shirt by layering PNG images on top of each other. For example, selecting a base color of 'blue' will show an image of a blue shirt. Then, selecting a body stitching of "ribbed" will place an image on top of the blue shirt that adds the ribbing detail. Right now, my problem is that each new image replaces previous ones. Everything else works fine (options appearing in response to previously selected options).

<div id="gender" class="diy-box">
    Pick Gender<br>
    <input type="radio" name="gender" data-id="" value="male" /><label>Male</label><br>
    <input type="radio" name="gender" data-id="" value="female" /><label>Female</label>

</div>

<section id="displaysection">

    <div id="male" class="desc gender diy-box">

        Pick Body<br>
        <input type="radio" name="male" data-id="105" value="" /><label>Blue</label><br>
        <input type="radio" name="male" data-id="120" value="" /><label>Black</label><br>
        <input type="radio" name="male" data-id="145" value="" /><label>White</label>

    </div>

    <div id="female" class="desc gender diy-box">

        Pick Body<br>
        <input type="radio" name="female" data-id="107" value="" /><label>Blue</label><br>
        <input type="radio" name="female" data-id="211" value="" /><label>Black</label><br>
        <input type="radio" name="female" data-id="212" value="" /><label>Pink</label>

    </div>

</section>

<div id="body_stitching" class="diy-box">

    Body Stitching<br>
    <input type="radio" name="body_stitching" data-id="" value="body_stitching_plain" /><label>Plain</label><br>
    <input type="radio" name="body_stitching" data-id="" value="body_stitching_rib" /><label>Rib</label>

</div>

<section id="displaysection">

    <div id="body_stitching_plain" class="desc body_stitching diy-box">

        Plain<br>
        <input type="radio" name="body_stitching_plain" data-id="324" value="" /><label>Blue</label><br>
        <input type="radio" name="body_stitching_plain" data-id="325" value="" /><label>Red</label>

    </div>

    <div id="body_stitching_rib" class="desc body_stitching diy-box">

        Rib<br>
        <input type="radio" name="body_stitching_rib" data-id="" value="black" /><label>Black</label><br>
        <input type="radio" name="body_stitching_rib" data-id="" value="green" /><label>Green</label>

    </div>

</section>

<div class="clear"></div>

<div id="shirt"></div>

<div id="pricefield" style="float:right"></div>

<script>

$(document).ready(function() {
    $("div.desc").hide();

    var data = {
        "105" : { img: "http://oceandrive.localhost/images/diy-images/105.png", label: "color 1", price: "100" },
        "120" : { img: "http://oceandrive.localhost/images/diy-images/120.png", label: "color 2", price: "110" },
        "145" : { img: "http://oceandrive.localhost/images/diy-images/145.png", label: "color 3", price: "120" },
        "107" : { img: "http://oceandrive.localhost/images/diy-images/107.gif", label: "color 4", price: "130" },
        "211" : { img: "http://oceandrive.localhost/images/diy-images/211.png", label: "color 5", price: "140" },
        "212" : { img: "http://oceandrive.localhost/images/diy-images/212.png", label: "color 6", price: "150" },
        "324" : { img: "http://oceandrive.localhost/images/diy-images/324.png", label: "color 7", price: "160" },
        "325" : { img: "http://oceandrive.localhost/images/diy-images/325.png", label: "color 8", price: "170" },
    };


    $('input[name]').click(function() {


        var value = $(this).val();   // pics the value of the radio button

        if(value=='male' || value=='female') {
            $("div.gender").hide('slow');
            $("div.gender input:radio").removeAttr('checked');
        }

        if(value=='body_stitching_plain' || value=='body_stitching_rib') {
            $("div.body_stitching").hide('slow');
            $("div.body_stitching input:radio").removeAttr('checked');
        }

        $("#" + value).show('slow');  // addresses the div with the radio button value picked



        if(this.checked) {

            //var value = $(this).val();
            var value = $(this).data('id');

            if (data[value] != undefined)
            {

                var html = '';
                html = html + '<img style="z-index:2;" src="'+data[value].img+'"/>';
                $('#shirt').html(html);

                var html = '';
                html = html + '- '+data[value].label+' - '+data[value].price+' NT<br>';
                $('#pricefield').html(html);

            }

        }

    });

});

解决方案

First, you need to decide what order your images should appear in. For example, from your code I would say you have two levels (you can add more levels if you need them):

  1. Base
  2. Body stitching

Second, assign each level to a z-index and create <div> elements for each one. These <div>'s should be styled so that their positioning is absolute, like so:

<div id="baseImage" style="z-index:1; position: absolute;"></div>
<div id="stitchImage" style="z-index:2; position: absolute;"></div>
...

I would probably also specify the width/height of each <div> to match your images.

Finally, use jQuery to position each <div> on top of the base image.

$("#stitchImage").offset($("#baseImage").offset());

Then, when you need to replace images, simply use the code that you have:

$("#stitchImage").html("<img src='" + src + "' />");

Note that you will have to use a blank placeholding image so that users can see lower layers when they haven't selected higher layers yet.

这篇关于Jquery:如何将图像叠加在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆