Foros del Web » Creando para Internet » HTML »

Mapear Imagen con div con tamaños dinamicos

Estas en el tema de Mapear Imagen con div con tamaños dinamicos en el foro de HTML en Foros del Web. Estoy trabajando en una imagen SVG, en la cual mediante divs flotantes quiero hacer un efecto hover en algunas posiciones del mapa, hasta ahi todo ...
  #1 (permalink)  
Antiguo 15/09/2015, 10:46
 
Fecha de Ingreso: septiembre-2007
Mensajes: 150
Antigüedad: 16 años, 7 meses
Puntos: 1
Mapear Imagen con div con tamaños dinamicos

Estoy trabajando en una imagen SVG, en la cual mediante divs flotantes quiero hacer un efecto hover en algunas posiciones del mapa, hasta ahi todo bien, el problema es que estoy usando una libreria para hacer un wheel mouse zoom y drag, lo que sucede es que cuando se redimensiona la imagen no se redimensionan los divs en razon del zoom ni se mantiene su posicion relativo al mapa.

http://www.salondental.cl/mapa-compra/

Código HTML:
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Wheelzoom</title>
	<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>
	<style>
	#capa-btn{ position:relative; width:300px; height:150px; border:2px solid #ccc; z-index:2; top: -690px; left: 100px; }
	.btn-a{ position: relative;	width: 20px; height: 20px; top: 40px; background: #ccc;	}
	</style>
</head>
<body>
	<img class='zoom' src='PLANO_SALONDENTAL_CASAPIEDRA_1_copia-1.svg' alt='Daisy!' width='100%' height='100%'/>
	<script src="wheelzoom.js"></script>
	<script>
		wheelzoom(document.querySelector('img.zoom'));
	</script>
</body>
</html> 
Código Javascript:
Ver original
  1. /*!
  2.     Wheelzoom 3.0.1
  3.     license: MIT
  4.     http://www.jacklmoore.com/wheelzoom
  5. */
  6. window.wheelzoom = (function(){
  7.     var defaults = {
  8.         zoom: 0.10
  9.     };
  10.  
  11.     var canvas = document.createElement('canvas');
  12.  
  13.     function setSrcToBackground(img) {
  14.         img.style.backgroundImage = 'url("'+img.src+'")';
  15.         img.style.backgroundRepeat = 'no-repeat';
  16.         canvas.width = img.naturalWidth;
  17.         canvas.height = img.naturalHeight;
  18.         img.src = canvas.toDataURL();
  19.     }
  20.  
  21.     var main = function(img, options){
  22.         if (!img || !img.nodeName || img.nodeName !== 'IMG') { return; }
  23.  
  24.         var settings = {};
  25.         var width;
  26.         var height;
  27.         var bgWidth;
  28.         var bgHeight;
  29.         var bgPosX;
  30.         var bgPosY;
  31.         var previousEvent;
  32.  
  33.         function updateBgStyle() {
  34.             if (bgPosX > 0) {
  35.                 bgPosX = 0;
  36.             } else if (bgPosX < width - bgWidth) {
  37.                 bgPosX = width - bgWidth;
  38.             }
  39.  
  40.             if (bgPosY > 0) {
  41.                 bgPosY = 0;
  42.             } else if (bgPosY < height - bgHeight) {
  43.                 bgPosY = height - bgHeight;
  44.             }
  45.  
  46.             img.style.backgroundSize = bgWidth+'px '+bgHeight+'px';
  47.             img.style.backgroundPosition = bgPosX+'px '+bgPosY+'px';
  48.         }
  49.  
  50.         function reset() {
  51.             bgWidth = width;
  52.             bgHeight = height;
  53.             bgPosX = bgPosY = 0;
  54.             updateBgStyle();
  55.         }
  56.  
  57.         function onwheel(e) {
  58.             var deltaY = 0;
  59.  
  60.             e.preventDefault();
  61.  
  62.             if (e.deltaY) { // FireFox 17+ (IE9+, Chrome 31+?)
  63.                 deltaY = e.deltaY;
  64.             } else if (e.wheelDelta) {
  65.                 deltaY = -e.wheelDelta;
  66.             }
  67.  
  68.             // As far as I know, there is no good cross-browser way to get the cursor position relative to the event target.
  69.             // We have to calculate the target element's position relative to the document, and subtrack that from the
  70.             // cursor's position relative to the document.
  71.             var rect = img.getBoundingClientRect();
  72.             var offsetX = e.pageX - rect.left - document.body.scrollLeft;
  73.             var offsetY = e.pageY - rect.top - document.body.scrollTop;
  74.  
  75.             // Record the offset between the bg edge and cursor:
  76.             var bgCursorX = offsetX - bgPosX;
  77.             var bgCursorY = offsetY - bgPosY;
  78.            
  79.             // Use the previous offset to get the percent offset between the bg edge and cursor:
  80.             var bgRatioX = bgCursorX/bgWidth;
  81.             var bgRatioY = bgCursorY/bgHeight;
  82.  
  83.             // Update the bg size:
  84.             if (deltaY < 0) {
  85.                 bgWidth += bgWidth*settings.zoom;
  86.                 bgHeight += bgHeight*settings.zoom;
  87.             } else {
  88.                 bgWidth -= bgWidth*settings.zoom;
  89.                 bgHeight -= bgHeight*settings.zoom;
  90.             }
  91.  
  92.             // Take the percent offset and apply it to the new size:
  93.             bgPosX = offsetX - (bgWidth * bgRatioX);
  94.             bgPosY = offsetY - (bgHeight * bgRatioY);
  95.  
  96.             // Prevent zooming out beyond the starting size
  97.             if (bgWidth <= width || bgHeight <= height) {
  98.                 reset();
  99.             } else {
  100.                 updateBgStyle();
  101.             }
  102.         }
  103.  
  104.         function drag(e) {
  105.             e.preventDefault();
  106.             bgPosX += (e.pageX - previousEvent.pageX);
  107.             bgPosY += (e.pageY - previousEvent.pageY);
  108.             previousEvent = e;
  109.             updateBgStyle();
  110.         }
  111.  
  112.         function removeDrag() {
  113.             document.removeEventListener('mouseup', removeDrag);
  114.             document.removeEventListener('mousemove', drag);
  115.         }
  116.  
  117.         // Make the background draggable
  118.         function draggable(e) {
  119.             e.preventDefault();
  120.             previousEvent = e;
  121.             document.addEventListener('mousemove', drag);
  122.             document.addEventListener('mouseup', removeDrag);
  123.         }
  124.  
  125.         function loaded() {
  126.             var computedStyle = window.getComputedStyle(img, null);
  127.  
  128.             width = parseInt(computedStyle.width, 10);
  129.             height = parseInt(computedStyle.height, 10);
  130.             bgWidth = width;
  131.             bgHeight = height;
  132.             bgPosX = 0;
  133.             bgPosY = 0;
  134.  
  135.             setSrcToBackground(img);
  136.  
  137.             img.style.backgroundSize =  width+'px '+height+'px';
  138.             img.style.backgroundPosition = '0 0';
  139.             img.addEventListener('wheelzoom.reset', reset);
  140.  
  141.             img.addEventListener('wheel', onwheel);
  142.             img.addEventListener('mousedown', draggable);
  143.         }
  144.  
  145.         img.addEventListener('wheelzoom.destroy', function (originalProperties) {
  146.             console.log(originalProperties);
  147.             img.removeEventListener('wheelzoom.destroy');
  148.             img.removeEventListener('wheelzoom.reset', reset);
  149.             img.removeEventListener('load', onload);
  150.             img.removeEventListener('mouseup', removeDrag);
  151.             img.removeEventListener('mousemove', drag);
  152.             img.removeEventListener('mousedown', draggable);
  153.             img.removeEventListener('wheel', onwheel);
  154.  
  155.             img.style.backgroundImage = originalProperties.backgroundImage;
  156.             img.style.backgroundRepeat = originalProperties.backgroundRepeat;
  157.             img.src = originalProperties.src;
  158.         }.bind(null, {
  159.             backgroundImage: img.style.backgroundImage,
  160.             backgroundRepeat: img.style.backgroundRepeat,
  161.             src: img.src
  162.         }));
  163.  
  164.         options = options || {};
  165.  
  166.         Object.keys(defaults).forEach(function(key){
  167.             settings[key] = options[key] !== undefined ? options[key] : defaults[key];
  168.         });
  169.  
  170.         if (img.complete) {
  171.             loaded();
  172.         } else {
  173.             img.addEventListener('load', function onload() {
  174.                 img.removeEventListener('load', onload);
  175.                 loaded();
  176.             });
  177.         }
  178.     };
  179.  
  180.     // Do nothing in IE8
  181.     if (typeof window.getComputedStyle !== 'function') {
  182.         return function(elements) {
  183.             return elements;
  184.         };
  185.     } else {
  186.         return function(elements, options) {
  187.             if (elements && elements.length) {
  188.                 Array.prototype.forEach.call(elements, main, options);
  189.             } else if (elements && elements.nodeName) {
  190.                 main(elements, options);
  191.             }
  192.             return elements;
  193.         };
  194.     }
  195. }());

Etiquetas: dinamicos, tamaños, todo
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 21:18.