Skip to content Skip to sidebar Skip to footer

Image Preview Box

I have an issue with images preview in my CSS (or jQuery?). Please look at the right example page in top and my messed page in bottom: Actually I want to keep my preview box in th

Solution 1:

Image preview on mouseover script

jQuery

this.imagePreview = function(){ 

    xOffset = 10;
    yOffset = 30;

    // these 2 variable determine popup's distance

$("a.preview").hover(function(e){
    this.t = this.title;
    this.title = "";    
    var c = (this.t != "") ? "<br/>" + this.t : "";
    $("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");                                
    $("#preview")
        .css("top",(e.pageY - xOffset) + "px")
        .css("left",(e.pageX + yOffset) + "px")
        .fadeIn("fast");                        
    },

function(){
    this.title = this.t;    
    $("#preview").remove();
    });

    $("a.preview").mousemove(function(e){
    $("#preview")
        .css("top",(e.pageY - xOffset) + "px")
        .css("left",(e.pageX + yOffset) + "px");
  });
};

starting the script on page load

$(document).ready(function(){
imagePreview();
});

Solution 2:

let preview = document.querySelectorAll(".preview");
let previewBoxImg = document.querySelector(".previewBox img");
let previewBox = document.querySelector(".previewBox");

for(let i = 0; i < preview.length; i++){
	
	
	preview[i].addEventListener("mouseenter",function(e){
		
		let imgSrc = this.href;
	  previewBoxImg.src = imgSrc;
		previewBox.classList.add("show");
		previewBox.style = `top: ${e.clientY};left: ${e.clientX};`;
		
	});
	
	preview[i].addEventListener("mouseleave",function(){
		
		previewBox.classList.remove("show");
		
	});
	
	
}
.previewBox{

  background: #212323;
  width: 10em;
  height: 10em;
  padding: 0.3em;
  display: none;
	position: relative;

}

.previewBox.show{

  display: block;
  

}

.previewBoximg{
  width: 100%;
  height: 100%;
}
<ahref="http://pre01.deviantart.net/ad77/th/pre/f/2013/181/3/8/rainbow_dash___equestria_girls_by_pixelflow1-d6bcnn3.png"target="_blank"class="preview">Rainbow Dash</a><ahref="http://img04.deviantart.net/bc84/i/2013/230/4/1/equestria_girls__wondercolt_applejack_by_deathnyan-d6ip4fx.png"target="_blank"class="preview">AppleJack</a><ahref="http://orig12.deviantart.net/e1d6/f/2013/297/f/d/equestria_girls_fluttershy_by_iamaquamarine_d6_by_laughandcry12-d6rozpu.png"target="_blank"class="preview">Fluttershy</a><ahref="http://t02.deviantart.net/3nw-fOth2hVnTnPyGp9b-CTMAdI=/fit-in/300x900/filters:no_upscale():origin()/pre03/4d4e/th/pre/i/2015/056/7/8/pinkie_pie_eqg__wondercolts_pose_by_caliazian-d6mznqo.png"target="_blank"class="preview">Pinkie</a><ahref="http://orig08.deviantart.net/ba65/f/2013/186/b/5/egq_rarity_vector_by_unicornrarity-d6c4uav.png"target="_blank"class="preview">Rarity</a><ahref="http://orig01.deviantart.net/6eb7/f/2014/003/b/2/twilight_sparkle_equestria_girls__blush__by_emerald_ray-d6qyey0.png"target="_blank"class="preview">Twilight</a><divclass="previewBox"><imgsrc="" /></div>

Post a Comment for "Image Preview Box"