javascript - jquery check if img is inside div -
// w s d $(document).ready(function(e){ var keys = {}; $(document).keydown(function(event){ keys[event.which] = true; }).keyup(function(event){ delete keys[event.which]; }); var $d = $("img"); function gameloop() { if (keys[68]) { $d.css("left", function(i,oldval) { return parseint(oldval) + 5 + "px"; }); } else if (keys[65]) { $d.css("left", function(i,oldval) { return parseint(oldval) - 5 + "px"; }); } if (keys[83]) { $d.css("top", function(i,oldval) { return parseint(oldval) + 5 + "px"; }); } else if (keys[87]) { $d.css("top", function(i,oldval) { return parseint(oldval) - 5 + "px"; }); } settimeout(gameloop, 20); } gameloop(); $(document).focus(); }); // arrows $(document).ready(function(e){ var keys = {}; $(document).keydown(function(event){ keys[event.which] = true; }).keyup(function(event){ delete keys[event.which]; }); var $d = $("img"); function gameloop() { if (keys[37]) { $d.css("left", function(i,oldval) { return parseint(oldval) - 5 + "px"; }); } else if (keys[39]) { $d.css("left", function(i,oldval) { return parseint(oldval) + 5 + "px"; }); } if (keys[40]) { $d.css("top", function(i,oldval) { return parseint(oldval) + 5 + "px"; }); } else if (keys[38]) { $d.css("top", function(i,oldval) { return parseint(oldval) - 5 + "px"; }); } settimeout(gameloop, 20); } gameloop(); $(document).focus(); }); $(document).ready(function(){ if($(".goal:has(img)")){ alert("yes"); } });
img { position : absolute; left: 0; top: 0; } .goal{ width: 10px; height: 10px; background-color: green; float: right; }
<!doctype html> <html> <head> <title>super mario!</title> <link rel='stylesheet' type='text/css' href='style.css'/> </head> <body> <img src="http://i1061.photobucket.com/albums/t480/ericqweinstein/mario.jpg"/> <div class="goal"></div> </body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <script src="script.js"></script> </html>
i want alert box pops when mario(img) inside goal(div). help. don't mind js code it's random , don't know if it's close right, thanks! edit: div green dot on top right corner.
i want alert box pops when mario(img) inside goal(div).
it seems, don't need check if element img
inside div
or not. want check if coordinates of mario img
within range of goal div
.
easiest use element.getboundingclientrect
, obtain domrect
object hold size , position of elements. use figure out if mario in range of goal.
create function check position every time keyboard navigation happens. this:
function checkmario() { var goalpost = $('.goal')[0].getboundingclientrect(); var mario = $('#mario')[0].getboundingclientrect(); if ((goalpost.left - mario.left) < 60) { $('#result').text('yesssss'); } else { $('#result').text('no yet'); } }
example snippet:
in example, have kept simple calculate if left
position within range of div
. need refine further check sides.
try using keyboard left/right keys move mario in goal.
$(document).ready(function(e) { var keys = {}; $(document).keydown(function(event) { keys[event.which] = true; }).keyup(function(event) { delete keys[event.which]; }); var $d = $("img"); function gameloop() { if (keys[68]) { $d.css("left", function(i, oldval) { return parseint(oldval) + 5 + "px"; }); } else if (keys[65]) { $d.css("left", function(i, oldval) { return parseint(oldval) - 5 + "px"; }); } if (keys[83]) { $d.css("top", function(i, oldval) { return parseint(oldval) + 5 + "px"; }); } else if (keys[87]) { $d.css("top", function(i, oldval) { return parseint(oldval) - 5 + "px"; }); } settimeout(gameloop, 20); } gameloop(); $(document).focus(); }); // arrows $(document).ready(function(e) { var keys = {}; $(document).keydown(function(event) { keys[event.which] = true; }).keyup(function(event) { delete keys[event.which]; }); var $d = $("img"); function gameloop() { if (keys[37]) { $d.css("left", function(i, oldval) { return parseint(oldval) - 5 + "px"; }); checkmario(); } else if (keys[39]) { $d.css("left", function(i, oldval) { return parseint(oldval) + 5 + "px"; }); checkmario(); } if (keys[40]) { $d.css("top", function(i, oldval) { return parseint(oldval) + 5 + "px"; }); checkmario(); } else if (keys[38]) { $d.css("top", function(i, oldval) { return parseint(oldval) - 5 + "px"; }); checkmario(); } settimeout(gameloop, 20); } gameloop(); $(document).focus(); }); function checkmario() { var goalpost = $('.goal')[0].getboundingclientrect(); var mario = $('#mario')[0].getboundingclientrect(); if ((goalpost.left - mario.left) < 60) { $('#result').text('yesssss'); } else { $('#result').text('no yet'); } }
img { position: absolute; left: 0; top: 0; width: 60px; } .goal { width: 10px; height: 10px; background-color: green; float: right; } p { margin-top: 64px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <img id='mario' src="http://i1061.photobucket.com/albums/t480/ericqweinstein/mario.jpg" /> <div class="goal"></div> <br/> <p id="result"></p>
Comments
Post a Comment