$(document).ready(function() {
var paint,
$img;
$(".color").click(function() {
paint = $(this).css('background-color');
$img = undefined;
})
$('.gallery').on('click', 'div', function() {
$img = $(this).clone();
paint = undefined;
});
$('.wrap .insert').click(function() {
$(this).css("border-color", paint);
});
$('.wrap').on('click', '.insert:not(.occupied)', function() {
if ($img) {
$(this).append($img).addClass('occupied');
$img = undefined;
}
});
});
$(document).ready(function() {
function randomColor() {
return '#' + ('000000' + Math.floor(Math.random() * 16777216).toString(16)).slice(-6);
};
$("h1").click(function() {
$('body').css('background', randomColor());
});
})
.wrap {
display: flex;
flex-direction: row;
flex-wrap: wrap;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-align-content: stretch;
-ms-flex-line-pack: stretch;
align-content: stretch;
-webkit-align-items: flex-start;
-ms-flex-align: start;
align-items: flex-start;
;
height: 100px;
width: 100p5;
border: 1px solid grey;
}
.insert {
height: 100px;
width: 100px;
border: 1px solid grey;
}
#one {
background-color: blue;
float: left;
}
#two {
background-color: red;
float: left;
}
#three {
background: green;
float: left;
}
.gallery>div {
display: inline-block;
}
.gal1 {
height: 100px;
width: 100px;
background: url("https://i.kinja-img.com/gawker-media/image/upload/s--_KjrmVSk--/c_fill,fl_progressive,g_north,h_358,q_80,w_636/19c12pvu8o9sljpg.jpg");
}
.gal2 {
height: 100px;
width: 100px;
background: url(http://www.purple-planet.com/communities/5/004/012/574/565//images/4608142514_255x230.jpg);
}
.gal3 {
height: 100px;
width: 100px;
background: url("http://image.shutterstock.com/display_pic_with_logo/892819/164734181/stock-vector-glossy-planets-colorful-vector-set-on-dark-sky-background-164734181.jpg");
}
.occupied {
border: 3px pink solid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="random_background">
<h1>click</h1>
<div class="color" id="one">blue</div>
<div class="color" id="two">red</div>
<div class="color" id="three">green</div>
<div class="wrap">
<div class="insert"></div>
<div class="insert"></div>
<div class="insert"></div>
</div>
<div class="gallery">
<div class="gal1"></div>
<div class="gal2"></div>
<div class="gal3"></div>
</div>
</footer>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</div>
如果我多次點擊同一圖像會怎麼樣 –
http://jsfiddle.net/arunpjohny/o7tyfa3f/2/? –
@阿倫。 謝謝你,那是一個聰明的解決方案。 如果用戶點擊一次圖像兩次,我認爲爲此目的,它不應該生成下一個圖像,但我不知道做什麼替代,所以這很好。 然而,重置函數來清空佔位符將會很有用。 不是迂腐,但你如何將圖像存儲在點擊變種,然後在點擊佔位符中顯示它? 順便說一句,抱歉可怕的佈局,它只是一個項目的練習頁面:-) –