I was writing this application in HTML , CSS and Javascript that creates a magnifier glass that will be used in one of 3 images (choosen by the user), but when i change from one image to another, using a Combobox the magnifier glass becomes awkward (repeats itself).
I tried to change the code in several ways but i still didn't found what's wrong with it!?
Here's the code:
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- CSS Code -->
<style>
*
{
box-sizing: border-box;
}
.img-magnifier-container
{
position:relative;
}
.img-magnifier-glass
{
position: absolute;
border: 3px solid #000000;
border-radius: 50%;
cursor: none;
/* Sets the size of the Magnifier Glass. */
width: 100px;
height: 100px;
}
</style>
<!-- JavaScript Code -->
<script>
function magnify(imgID, zoom)
{
var img, glass, w, h, bw;
img = document.getElementById(imgID);
// Creates the Magnifier Glass.
glass = document.createElement("DIV");
glass.setAttribute("class", "img-magnifier-glass");
// Inserts the Magnifier Glass.
img.parentElement.insertBefore(glass, img);
// Sets the Background Properties for the Magnifier Glass.
glass.style.backgroundImage = "url('" + img.src + "')";
glass.style.backgroundRepeat = "no-repeat";
glass.style.backgroundSize = (img.width * zoom) + "px " + (img.height * zoom) + "px";
bw = 3;
w = glass.offsetWidth / 2;
h = glass.offsetHeight / 2;
// Executes the "moveMagnifier" Function when someone moves the Magnifier Glass over the Image.
glass.addEventListener("mousemove", moveMagnifier);
img.addEventListener("mousemove", moveMagnifier);
function moveMagnifier(e)
{
var pos, x, y;
// Prevents any other actions that may occur when moving over the Image.
e.preventDefault();
// Gets the Cursor's x and y Positions.
pos = getCursorPos(e);
x = pos.x;
y = pos.y;
// Prevents the Magnifier Glass from being positioned outside the Image.
if (x > img.width - (w / zoom))
x = img.width - (w / zoom);
if (x < w / zoom)
x = w / zoom;
if (y > img.height - (h / zoom))
y = img.height - (h / zoom);
if (y < h / zoom)
y = h / zoom;
// Sets the position of the Magnifier Glass.
glass.style.left = (x - w) + "px";
glass.style.top = (y - h) + "px";
// Displays what the Magnifier Glass sees.
glass.style.backgroundPosition = "-" + ((x * zoom) - w + bw) + "px -" + ((y * zoom) - h + bw) + "px";
}
function getCursorPos(e)
{
var a, x = 0, y = 0;
e = e || window.event;
// Gets the x and y Positions of the Image.
a = img.getBoundingClientRect();
// Calculates the Cursor's x and y Coordinates, relative to the Image.
x = e.pageX - a.left;
y = e.pageY - a.top;
// Consider any page scrolling.
x = x - window.pageXOffset;
y = y - window.pageYOffset;
return {x : x, y : y};
}
}
</script>
</head>
<body>
<h1>Image Magnifier Glass</h1>
<select id="CBFotos">
<option value="foto1.jpg">Foto 1</option>
<option value="foto2.jpg">Foto 2</option>
<option value="foto3.jpg">Foto 3</option>
</select>
<br><br><br>
<div class="img-magnifier-container">
<img id="foto" src="foto1.jpg" width="600" height="400">
</div>
<script>
// The User can choose from 3 Photos to use with the Magnifier Glass.
// When there is a change in the selected Item of the ComboBox, the "onchange" Event is triggered, and the declared Function is executed.
document.getElementById("CBFotos").onchange = function()
{
var comboBoxFotos = document.getElementById("CBFotos");
// Puts the new ComboBox's Selected Value in the "src" Attribute of the Img Element with the "id=foto".
document.getElementById("foto").attributes["src"].value = comboBoxFotos.options[comboBoxFotos.selectedIndex].value;
magnify("foto", 3);
};
// Calls the "magnify" Function with the "id" of the Image, and the strength of the Magnifier Glass.
magnify("foto", 3);
</script>
</body>
magnify()(each time you change the comboBox), you create a new div - which holds a new magnifying glass. It is doing what you've told it to do.