Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
Agregar zoom en la función de desplazamiento a todos los elementos de una clase

Quiero agregar el efecto de zoom flotante a todas las imágenes que pertenecen a la clase "img-container".

Para lograr este objetivo, estoy usando este código:

 var container = document.getElementsByClassName('img-container'); console.log(container.length); window.addEventListener("load", function(){ for(var i = 0; i < container.length; i++){ var current_img = container[i]; var imgsrc = current_img.currentStyle || window.getComputedStyle(current_img, false), imgsrc = imgsrc.backgroundImage.slice(4, -1).replace(/"/g, ""), img = new Image(); img.src = imgsrc; img.onload = function(){ var imgWidth = img.naturalWidth, imgHeight = img.naturalHeight, ratio = imgHeight/imgWidth, percentage = ratio * 100 + "%"; current_img.onmousemove = function(e){ var boxWidth = current_img.clientWidth, xPos = e.pageX - this.offsetLeft, yPos = e.pageY - this.offsetTop, xPercent = xPos / (boxWidth / 100) + "%", yPercent = yPos / (boxWidth * ratio / 100) + "%"; Object.assign(current_img.style, { backgroundPosition: xPercent + " " + yPercent, backgroundSize: imgWidth + "px" }); }; }; current_img.onmouseleave = function(e){ Object.assign(current_img.style, { backgroundPosition: "center", backgroundSize: "cover" }); }; }; });

Sin embargo, esto no funciona y no veo el error lógico que realicé. Este código funciona bien cuando uso getElementById en lugar de class, pero usar eso me obligaría a repetir el código muchas veces, lo cual es inviable.

about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!