1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| <!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Déplacement images</title>
<meta name="Author" content="NoSmoking">
<style>
html, body{
margin:0;
padding:0;
font: 1em/1.5 Verdana, sans-serif;
}
#main {
position:relative;
width:60em;
margin:0 auto;
}
h1 {
color:#069;
}
#main_dest {
width:320px;
margin:0 auto;
}
#main_dest div {
box-sizing:border-box;
float:left;
width:150px;
height:100px;
border:1px dashed #ccc;
margin:5px;
}
#main_img {
height:10em;
}
#main_img li {
width:10em;
height:10em;
float:left;
list-style: none;
}
#main_img img {
position:relative;
width:150px;
max-height:100px;
cursor:pointer;
}
#main_img img:hover {
box-shadow:1px 1px 20px #ccc;
}
</style>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="main">
<h1>Déplacement images au clic</h1>
<div id="main_img">
<ul>
<li><img src="http://club.developpez.com/webdesign/Rubriques/Web/JavaScript/logo_faq_tous_115.gif" alt=""></li>
<li><img src="http://club.developpez.com/webdesign/Rubriques/Web/JavaScript/logo_faq_google_maps_150.gif" alt=""></li>
<li><img src="http://club.developpez.com/webdesign/Rubriques/Web/JavaScript/faq_jquery_tiny.gif" alt=""></li>
<li><img src="http://club.developpez.com/webdesign/Rubriques/Web/JavaScript/faq_senchaextjs_tiny.gif" alt=""></li>
</ul>
</div>
<div id="main_dest">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</div>
<script>
$(function()
{
var tab_dest = $("#main_dest").find("div"),
tab_dest_length = tab_dest.length,
i_dest = 0;
$("#main_img").find('img').one('click', function()
{
if(i_dest < tab_dest_length)
{
var $dest = tab_dest.eq(i_dest),
pos = $dest.offset(),
thisPos = $(this).offset();
i_dest++;
$dest.css('background-color', '#f8f8fa');
$(this).animate({
left: '+=' + (pos.left - thisPos.left),
top: '+=' + (pos.top - thisPos.top)
},
1000,
function() {
$dest.css('background-color', '');
}
);
}
});
})
</script>
</body>
</html> |
Partager