The mouse hover is showing the <div>s normally but after the first time I move the mouse it no longer shows the other <div>s. It eventually stays on the last image and doesn’t change to another.
I tried with fadeToggle but I would like it to be fixed to the last image that appeared, but fadeToggle hides the last image when you mouse out.
$('.second').hover(function() {
$('.showsecond').fadeIn();
});
$('.third').hover(function() {
$('.showthird').fadeIn();
});
$('.fourth').hover(function() {
$('.showfourth').fadeIn();
});
$('.fifth').hover(function() {
$('.showfifth').fadeIn();
});
.menuimage {
display: none;
position: absolute;
}
.menuimagefirst {
display: block !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-6">
<ul>
<li class="first">first</li>
<li class="second">second</li>
<li class="third">third</li>
<li class="fourth">fourth</li>
<li class="fifth">fifth</li>
</ul>
</div>
<div class="col-6">
<div class="menuimage menuimagefirst showfirst">
<img src="https://i.imgur.com/o87PUwV.jpg" />
</div>
<div class="menuimage showsecond">
<img src="https://i.imgur.com/ICs7TVH.jpg" />
</div>
<div class="menuimage showthird">
<img src="https://i.imgur.com/n9gdy8N.jpg" />
</div>
<div class="menuimage showfourth">
<img src="https://i.imgur.com/o87PUwV.jpg" />
</div>
<div class="menuimage showfifth">
<img src="https://i.imgur.com/ICs7TVH.jpg" />
</div>
</div>
https://jsfiddle.net/castordida/fwou1hvg/17/
I expected it to always show the last image and keep that image until I hover on another link.
UPDATE 29/08/19:
Sorry i did not mention that what i am using is in the wordpress menu, so i made use of classes and not data-toggle unfortunately. I tried to add data-toggle unsuccessfully to wordpress menu.
A bit a dirty work, but should work ;)
P.S : You can ignore .menu-item css as its just for aesthetics. Though rest of the css are important.
$('.menu-item').mouseenter(function()
{
var $this = $(this);
var hover_elem_classes = $this.attr("class");
var class_list;
class_list = hover_elem_classes.split(" ");
var currentImageClass="";
for(var i in class_list)
{
var styleClass = class_list[i];
if(styleClass.startsWith("show"))
{
currentImageClass = styleClass;
break;
}
}
var $activeImg = $(".menuimage."+currentImageClass);
if(!$activeImg.hasClass("active"))
{
$(".menuimage.active").stop().fadeOut().removeClass("active");
$activeImg.addClass("active").fadeIn();
}
});
.menuimage{display:none;position:absolute;}
.menu-item{padding:5px 10px;list-style-type:none;display:inline;text-transform:uppercase;}
.menuimage.active{z-index:10;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-6">
<ul>
<li class="menu-item showfirst adasd adasd">first</li>
<li class="menu-item showsecond zxczxc xczxczc">second</li>
<li class="menu-item showthird fghfgh ghfgh">third</li>
<li class="menu-item showfourth adasdg gfhfgh">fourth</li>
<li class="menu-item showfifth zxczxc czxc">fifth</li>
</ul>
</div>
<div class="col-6">
<div class="menuimage active showfirst" style="display:block;">
<img src="https://i.imgur.com/o87PUwV.jpg" />
</div>
<div class="menuimage showsecond">
<img src="https://i.imgur.com/ICs7TVH.jpg" />
</div>
<div class="menuimage showthird">
<img src="https://i.imgur.com/n9gdy8N.jpg" />
</div>
<div class="menuimage showfourth">
<img src="https://i.imgur.com/o87PUwV.jpg" />
</div>
<div class="menuimage showfifth">
<img src="https://i.imgur.com/ICs7TVH.jpg" />
</div>
</div>
Problem is you do not hide elements after they are shown so when you have an element on top of another element it will not be visible. So you need to hide them.
$("li[data-toggles]").on("mouseenter", function () {
$(".menuimage.active").stop().hide();
$($(this).data('toggles')).stop().fadeIn().addClass('active');
})
.menuimage {
display: none;
position: absolute;
}
.menuimagefirst {
display: block !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-6">
<ul>
<li class="first" data-toggles=".showfirst">first</li>
<li class="second" data-toggles=".showsecond">second</li>
<li class="third" data-toggles=".showthird">third</li>
<li class="fourth" data-toggles=".showfourth">fourth</li>
<li class="fifth" data-toggles=".showfifth">fifth</li>
</ul>
</div>
<div class="col-6">
<div class="menuimage menuimagefirst showfirst">
<img src="https://i.imgur.com/o87PUwV.jpg" />
</div>
<div class="menuimage showsecond">
<img src="https://i.imgur.com/ICs7TVH.jpg" />
</div>
<div class="menuimage showthird">
<img src="https://i.imgur.com/n9gdy8N.jpg" />
</div>
<div class="menuimage showfourth">
<img src="https://i.imgur.com/o87PUwV.jpg" />
</div>
<div class="menuimage showfifth">
<img src="https://i.imgur.com/ICs7TVH.jpg" />
</div>
</div>
You've got a lot of unnecessary code here and it's contributing to your issues.
There is no need to create an img element for each picture you wish to show. Since you only want to show one image at a time, one img element is all you need. You'll just programatically change its source as needed. The image paths themselves will be kept in an array and a counter will be used to keep track of which index from the array to use as the source for the image element.
By eliminating the multiple img elements, you can eliminate the CSS that positions them, which is the source of the issue you are currently facing (layering of images).
Also, don't use a elements unless you are navigating somewhere when they are clicked as a elements are exclusively for navigation. Instead, you can set up your event on the li element that the user will be mousing over.
let imgs = [
"https://i.imgur.com/o87PUwV.jpg",
"https://i.imgur.com/ICs7TVH.jpg",
"https://i.imgur.com/n9gdy8N.jpg",
"https://i.imgur.com/o87PUwV.jpg",
"https://i.imgur.com/ICs7TVH.jpg"
];
let idx = 0;
$('.imgNav').on("mouseover", function() {
$(".menuimage").fadeOut(function(){
// As long as the index is less than the highest one in the array...
if(idx < (imgs.length - 1)){
// Set the source of the image element to the next path in the array
$(".menuimage")[0].src = imgs[++idx];
} else {
// Otherwise, reset the index
idx = 0;
}
// Show the image element now that its source has been changed.
$(".menuimage").fadeIn();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-6">
<ul>
<li class="imgNav">first</li>
<li class="imgNav">second</li>
<li class="imgNav">third</li>
<li class="imgNav">fourth</li>
<li class="imgNav">fifth</li>
</ul>
</div>
<div class="col-6">
<img class="menuimage" src="https://i.imgur.com/o87PUwV.jpg">
</div>
Related
I encountered this problem while making an online shopping mall page.
Most of the photos uploaded to my db have different sizes and I had to resize them all. But when I did, some of the pictures' resolutions became really bad when I set all of their height the same.
I wanna add something like an if clause that determines size of the image then resizes only the images which are bigger than the given size.
I want to add something looks like this, I guess:
if (img height < 300px) {
img height = "100px";
} else {
img height = "400px";
}
Here's my current code:
<ul class="prdList column4">
<li class="item" id="anchorBoxId_{$product_no}">
<div class="box">
<span class="chk"><input type="checkbox" class="{$product_compare_class} {$product_compare_display|display}" /></span>
<img src="{$image_big}" id="{$image_medium_id}" alt="" class="thumb" />
<div class="status">
<div class="icon">{$soldout_icon} {$recommend_icon} {$new_icon} {$product_icons} {$benefit_icons}</div>
<div class="button"><div class="option">{$option_preview_icon}</div> {$basket_icon} {$zoom_icon}</div>
</div>
<p class="name">
<strong class="title {$product_name_title_display|display}">{$product_name_title} :</strong> {$product_name}
</p>
<ul module="product_ListItem">
<li class="{$item_display|display}"><strong class="title {$item_title_display|display}">{$item_title} :</strong> {$item_content}</li>
<li class="{$item_display|display}"><strong class="title {$item_title_display|display}">{$item_title} :</strong> {$item_content}</li>
</ul>
</div>
</li>
<li class="item" id="anchorBoxId_{$product_no}">
<div class="box">
<span class="chk"><input type="checkbox" class="{$product_compare_class} {$product_compare_display|display}" /></span>
<img src="{$image_big}" id="{$image_medium_id}" alt="" class="thumb" />
<div class="status">
<div class="icon">{$soldout_icon} {$recommend_icon} {$new_icon} {$product_icons} {$benefit_icons}</div>
<div class="button"><div class="option">{$option_preview_icon}</div> {$basket_icon} {$zoom_icon}</div>
</div>
<p class="name">
<strong class="title {$product_name_title_display|display}">{$product_name_title} :</strong> {$product_name}
</p>
<ul module="product_ListItem">
<li class="{$item_display|display}"><strong class="title {$item_title_display|display}">{$item_title} :</strong> {$item_content}</li>
<li class="{$item_display|display}"><strong class="title {$item_title_display|display}">{$item_title} :</strong> {$item_content}</li>
</ul>
</div>
</li>
</ul>
</div>
I want to add something looks like this, I guess:
if (img height < 300px) {
img height = "100px";
} else {
img height = "400px";
}
You guess it right.
You just need to find the functions to detect and change image size.
Where to find these functions
First check the function reference in official PHP manual.
Then you will find a dedicated section on image processing:
Image Processing and Generation
It turns out that several libraries provide required functions.
For example, if you are using GD, it offers getimagesize and imagescale.
Gmagick and ImageMagick have similar functions, just with different names.
I plan on deleting all instances of an entire <div> from the page, if it has an image with a specific name. I am not sure if it's even possible with JQuery.
Here is my HTML:-
<div class="jk-content-module__image">
<a href="path-to-img" title="" class="colorbox init-colorbox-processed cboxElement" rel="gallery-node-2">
<img typeof="foaf:Image" src="path-to-img" alt="" title="">
</a>
<div style="display: none;">
<div id="colorbox-inline-1">
<ul>
<li class="first last"><img typeof="foaf:Image" src="http://thesite.com/sites/default/files/default_images/blank_0.png" alt="" title=""></li>
</ul>
</div>
</div>
</div>
What I am hoping is, that all the <div> on my page with a class of jk-content-module__image should have the child <div style="display: none;"> ... </div> removed, if there is an <img> with a src of blank_0.png in it.
Hence if the image has a src of blank_0.png present, I want the entire <div style="display: none;"> to be removed.
I hope I am clear.
I'll be grateful if you could assist.
You can use .filter() on ".jk-content-module__image" elements with parameter :has(img[src=blank_0.png]), .find() with parameter "div[style*='display: none']" note space character before none which matches current html, .remove()
$(function() {
$(".jk-content-module__image").filter(":has(img[src*='blank_0.png'])")
.find("div[style*='display: none']").remove()
})
jsfiddle https://jsfiddle.net/ks6b596n/1/
You can do like this:
$(document).ready(function() {
var elements = $(".jk-content-module__image");
elements.each(function(i, obj) {
if ($(obj).find("div[style*='display: none'] img").attr("src").indexOf("blank_0.png") > 0) {
$(obj).find(".hiddenDiv").remove();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="jk-content-module__image">
<a href="path-to-img" title="" class="colorbox init-colorbox-processed cboxElement" rel="gallery-node-2">
<img typeof="foaf:Image" src="path-to-img" alt="" title="">
</a>
<div class="hiddenDiv" style="display: none;">
<div id="colorbox-inline-1">
<ul>
<li class="first last">
<a href="http://thesite.com/sites/default/files/default_images/blank_0.png" title="" class="colorbox init-colorbox-processed cboxElement" rel="gallery-node-2">
<img typeof="foaf:Image" src="http://thesite.com/sites/default/files/default_images/blank_0.png" alt="" title="">
</a>
</li>
</ul>
</div>
</div>
</div>
maybe something like this with JQuery?
//get the src for your image
var imgSrc = $(".jk-content-module__image div img").attr('src').toLowerCase()
//see if it contains blank_0
if(imgSrc.indexOf('blank_0.png')>-1)
{
//if true, remove the div
$(".jk-content-module__image div")
}
You can use this:
$(".jk-content-module__image div:hidden").filter(function() {
return $(this).find('img').attr('src').indexOf('blank_0.png') !== -1
}).remove()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="jk-content-module__image">
<a href="path-to-img" title="" class="colorbox init-colorbox-processed cboxElement" rel="gallery-node-2">
visible image
<img typeof="foaf:Image" src="path-to-img" alt="" title="">
</a>
<div style="display: none;">
<div id="colorbox-inline-1">
<ul>
<li class="first last">
<img typeof="foaf:Image" src="http://thesite.com/sites/default/files/default_images/blank_0.png" alt="" title=""> hidden img
</li>
</ul>
</div>
</div>
</div>
Fiddle
Using jQuery:
$('img[src~=/blank_0.png]').closest('div[style*="display: none"]').remove();
In English: Find all img elements who's src attributes end in /blank_0.png. From there, find their closest ancestor elements who's style attributes contain display: none. Remove those ancestor elements.
May need to adjust the spacing in display: none as this needs to be exactly as it appears in the original.
In javascript:
[].slice.call(document.getElementsByTagName('IMG')).filter(function(e,i,a){
return e.src.endsWith('/blank_0.png');
}).forEach(function(e,i,a){
var t = e.parentNode;
while(t != null && (t.nodeName != 'DIV' || t.style.display != 'none')){
t = t.parentNode;
}
if(t != null && t.parentNode != null) {
t.remove();
}
});
I am learning js and jquery and have been trying to solve this litle issue I am having when trying to loop through a ul on the page and move a dom element specifically an image up two levels in my html. Here is what the html looks like:
<ul class="feedEkList">
<li class="slick-slide">
<div class="featured-title">
<div class="post-title">
some text
</div>
<div class="post-excerpt">
<img src="imageurl.jpg" class="wp-post-image">
<p>Post Description.</p>
</div>
</div>
</li>
</ul>
There are 10 li items in my ul but for readability purpose I only included one so you can see my html structure.
My javascript seems to be adding all the images before every featured-title div giving me piles of pictures instead of the one that is associated with that li.
$(function() {
$('.wp-post-image').insertBefore($('.featured-title'));
});
So then I tried this using each but it does not seem to do anything.
$(function() {
$('.feedEkList > li > .featured-title > .post-excerpt > .wp-post-image').each(function() {
$('.wp-post-image').insertBefore($('.featured-title'));
});
});
I was thinking maybe i need to use "this" in the code somewhere but I am new to javascript and not sure how to properly use "this" with the insert before, or if "this" is even the best option.
$('this.wp-post-image').insertBefore($('this.featured-title'));
Can anyone help me figure out what I am doing wrong? It is driving me nuts!
You need to iterate each image
For each image, get his parent .slick-slide.
Prepend the image to the .slick-slide using .prepend
Optional: If you need to insertBefore .featured-title see the comment.
$('.wp-post-image').each(function() {
var $this = $(this),
parentLi = $this.parents('.slick-slide').first();
$this.prependTo(parentLi);
//$this.insertBefore(parentLi.find('.featured-title'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="feedEkList">
<li class="slick-slide">
<div class="featured-title">
<div class="post-title">
some text
</div>
<div class="post-excerpt">
<img src="http://i.stack.imgur.com/FgiPG.jpg" class="wp-post-image" />
<p>Post Description.</p>
</div>
</div>
</li>
<li class="slick-slide">
<div class="featured-title">
<div class="post-title">
some text
</div>
<div class="post-excerpt">
<img src="http://i.stack.imgur.com/FgiPG.jpg" class="wp-post-image" />
<p>Post Description.</p>
</div>
</div>
</li>
<li class="slick-slide">
<div class="featured-title">
<div class="post-title">
removed the "wp-post-image" class to show how it was look before
</div>
<div class="post-excerpt">
<img src="http://i.stack.imgur.com/FgiPG.jpg" />
<p>Post Description.</p>
</div>
</div>
</li>
</ul>
<div id="one-id">
<div id="some">Info</div>
<div id="control">
<div id="value-1">
<img id="image-one-id" />
<img id="image-two-id" />
<img id="image-three-id" />
</div>
<!-- this block appears after mouseover on <img id="image-x-id" />, for each image-x-id will be loaded its own values -->
<div id="value-2">
<div id="value-one-id"></div>
<div id="value-two-id"></div>
<div id="value-three-id"></div>
</div>
</div>
</div>
<div id="two-id">
same logic as above
</div>
etc...
Step 1:<div id="value-2"> will be shown by observing mouseover event on <img id="image-x-id" /> and all values stay visible. <img id="image-x-id" /> will get class="selected".
Step 2: By mouseover <img id="image-x-id" /> inside <div id="two-id"> the <div id="value-2"> inside <div id="one-id"> must disapear and Step 1 will be repeated for <div id="two-id">.
I need a little help, because i'm lost in my nested divs.
In JS:
$('.value1').find("img").hover(function(){
var el = '.' + $(this).attr('data-target');
$('.control').find(el).hide();
$(this).closest('.control').find(el).show();
});
In Css:
.value1{
background: yellow;
}
.value2{
display: none;
background: #ccc;
}
JSFIDDLE
I've got two divs - each with one image inside. If I click on the image inside the second div I want this image to become the content of the first div (like a gallery).
It's important that I don't replace the links inside the img tag, so document.IMG1name.src=document.IMG2name.src isn't a possibility.
I tried it with innerhtml, but it doesnt work:
<div id="container1">
<img src="blablabla">
</div>
<div id="container2"; onclick="document.getElementById('container1').innerHTML=document.getElementById('container2').innerHTML"><img src="../funnycat.jpg">
</div>
mm... your code works fine, just close img tags and remove semicolon:
<div id="container1">
<img src="blablabla" />
</div>
<div id="container2" onclick="document.getElementById('container1').innerHTML=document.getElementById('container2').innerHTML"><img src="https://www.google.es/logos/doodles/2013/holiday-series-2013-3-4504416610680832-hp.jpg" />
</div>
here you have a demo: http://jsfiddle.net/HLs86/
HTML
<div class="pricing_table" id="monthly">
<ul>
<li>Basic</li>
<li>Subscribe Now</li>
</ul>
<ul style="background-color:#CCCCCC;">
<h2>Monthly Plans</h2><a class='plansSwitch' href='#yearly'>Click here to switch to the "Yearly Plans"</a></ul>
</div>
<div class="pricing_table" id="yearly">
<ul>
<li>Basic</li>
<li>Subscribe Now</li>
</ul>
<ul style="background-color:#CCCCCC;">
<h2>Yearly Plans</h2><a class='plansSwitch' href='#monthly'>Click here to switch to the "Monthly Plans"</a></ul>
</div>
JS
var $plansHolders = $('#monthly, #yearly').hide();
$('#monthly').show();
$('.plansSwitch').click(function() {
var href = $(this).attr('href');
$plansHolders.hide();
$(href).show();
});
you just need to add javascript: on your onclick action like this
<div id="container1"></div>
<div id="container2" onclick="javascript:document.getElementById('container1').innerHTML=document.getElementById('container2').innerHTML"><img src="http://i.imgur.com/0fS8dCsb.jpg"/>
</div>
there is example