Here is a very simple code that works only with the axis xto show it by injecting an element imgwith the url value of the background image as srcwell as determining the height and width of the background image to calculate if a click has occurred on the background image or not.
This code needs tons of improvement. It does not work in the axis y. Also not involved background-positionand background-size. But itβs easy to add these futures.
Here is the fiddle :
And here is the jQuery code:
$('#d').bind('click', function(e){
var d = $(this),
bg = {};
$('body').append(
$('<img/>').attr('src',
d.css('background-image').split('(')[1].split(')')[0]
).attr('class', 'testImage'));
bg.h = $('.testImage').height();
bg.w = $('.testImage').width();
console.log(bg, e.offsetX, $('.testImage').width());
if(e.offsetX > $('.testImage').width()){
$('#l').text('it was NOT on background-image');
}
else{
$('#l').text('it was on background-image');
}
$('.testImage').hide();
})
source
share