jquery php assign css values dynamically - php

i have some images in an un ordered list.
<ul>
<li id='chain'>
<a href="">
<span class='fade'></span>
<div class='title'>TITLE</div>
<img src="img/bike/chain.jpg" alt="" width="550" height="440" />
</a>
</li>
<li id='grips'>
<a href="">
<span class='fade'></span>
<div class='title'>TITLE</div>
<img src="img/bike/grips.jpg" alt="" width="275" height="440" />
</a>
</li>
<li id='tires'>
<a href="">
<span class='fade'></span>
<div class='title'>TITLE</div>
<img src="img/bike/tires.jpg" alt="" width="175" height="220" />
</a>
</li>
</ul>
i need css that will give the span and div in each set the same width like this expressed in a loop, NOT hand written every item
#chain .fade, #chain .title {width:550px}
#grips .fade, #grips .title {width:275px}
#tires .fade, #tires .title {width:175px;}
question: could i dynamically create css using jQuery? if not how can i create this css and assign the width value using PHP?the list items will change often and i wish to use a loop or .each statement.
in other words i dont want to make a list of each item
is there a way to pull the li ID and the IMG dimension and dynamically create css
pseudo code:
var w = $('ul li').each(find img width).assign it to> .css(this .fade, this .title {width:w + "px"});
or
should i look into php to echo the output of each ul li IMG?

jQuery easily allows you to create inline CSS styles (styles that are applied to each element).
$('#chain, #grips, #tires').each(function(index, element){
$('.fade', element).width( $('img', element).width() );
});
Here is what this script is doing
.each() iterates over all the selected elements. index, as the name suggests, is the index of the array being iterated over. element is the current element being iterated over.
$('.fade', element) this looks for the class of fade within the element.
.width() will either get or set the width of an element. It will be set if a parameter is passed into it, example: .width(15). (This will set the width of the element to 15 pixels wide.) It retrieves the width of an element if nothing is passed into it, example: .width().
$('img', element).width() retrieves the width of the img tag inside the element. Because this value is being passed into $('.fade', element).width(), it will set elements with the class of fade to width of the img.

This is how to do it with jQuery:
$('li').each(function(){
var $li = $(this);
var $img = $li.find('img');
var width = $img.attr('width');
$li.find('.fade, .title').css('width', width + 'px');
});
Or get rid of the jQuery, and use just css:
​li { float: left; clear: left; }
http://jsfiddle.net/u2HNx/2/

ok, try this:
$('li').each(function(){
var width = $(this).find('IMG').width();
$(this).find('.fade').width(width);
$(this).find('.title').width(width);
});

$("#chain .fade,#chain .title").css('width','550px');

Related

add auto increment value to li element using jQuery

I have the following code that its been dynamically generated by a plugin, the plugin will create a "li" element for each team member, currently i have 150 members so it needs to add an auto increment ID. Thanks!
<ul id="dvteamgrid<?php echo esc_attr($random); ?>" class="dvteamgrid withanim">
<li data-filter-class='["gridall",<?php echo $filters; ?>]'>
<img src="<?php echo esc_url($thumb_url); ?>" alt="<?php the_title(); ?>"/>
</li>
This is how the html code looks like:
<ul id="dvteamgrid225306813" class="dvteamgrid withanim">
<li data-filter-class="["gridall","dvfilter5997", "dvfilter6007", "dvfilter6025";]">
<img src="/wp-content/uploads/2015/03/Abee-C.jpg">
</li>
</ul>
You need to loop through each li element and attach the auto incremented id.
var auto_inc_id = 0; //initialise value of auto incremented id to 0
$('ul.dvteamgrid li').each(function(){ //loop through each list element of ul
$(this).attr('id', auto_inc_id); //attach to current li an id attribute that has auto_inc_id as its value
auto_inc_id++; //increment auto_inc_id by one
});

JQuery hover related information for an image from database based on ID

I have a 'post' row in my database where all the posted posts are stored.
Each post includes an id, image, title, description. I retrieve it to the main page using a while loop and I want to create a hover effect on each image that will display its corresponding information (the title and the description) based on its id.
I tried a few things with jquery but didn't seem to go anywhere.
Basic version of the database row displayed on html:
<img src="http://www.extremetech.com/wp-content/uploads/2012/12/VWXL1-1024-640x426.jpg" class="post" id="1">
<div class="ad_fade" id="fade1"></div>
<img src="http://www.hdwallpaperscool.com/wp-content/uploads/2013/11/peter-pan-cars-sports-car-hd-wallppers-cool-desktop-images.jpg" class="post" id="2">
<div class="ad_fade" id="fade2"></div>
<img src="http://www.hdwallpapers.in/walls/corvette_mallett_concept_car-HD.jpg" class="post" id="3">
<div class="ad_fade" id="fade3"></div>
in the php code I have each img tag have an id setup like so: id="'.$id.'" where $id is a unique id returned from the database
the styling:
.post{
width:100px;
height:100px;
}
.ad_fade{
display:none;
position:absolute;
margin:-105px 0 0;
width:100px;
height:100px;
background:rgba(255,255,255,0.8);
}
http://jsfiddle.net/g45db/
I am trying to create a similar effect as seen on: http://dribbble.com/
It has to hover the related info for that specific image in a dynamic way.
All help is appreciated!
Try it like,
$(function(){
$('img.post').hover(function(){
$(this).next('div').show().html(this.src);
// you can use ajax to get data from database
});
});
Live Demo
Updated try to use mouseleave on div.ad_fade like
$(function () {
$('img.post').hover(function () {
$(this).next('div').show().html($(this).data('title')); // using title here
// you can use ajax to get data from database
});
$('div.ad_fade').on('mouseleave',function(){ // mouse leave for fadeout div
$(this).fadeOut(1000).html('');
});
});
Updated Demo
You either need container to hold title and description on hover of img, create as two separate divs
Try to store information in img tag as data-title and data-description and then apply onmouse over function of jquery to show hover effects
$('img').onmouseover(function(){
var $this = $(this);
var title = $this.data('title');
// try to append title and description in previously created divs
});
The simplest way to display title and description, use title attribute of img tag like below
<img src="http://www.extremetech.com/wp-content/uploads/2012/12/VWXL1-1024-640x426.jpg" class="post" id="1" title="Title and Description">
And if you want to use jquery then use below code. First populate title and description in each for respective image.
<img src="http://www.extremetech.com/wp-content/uploads/2012/12/VWXL1-1024-640x426.jpg" class="post" id="1">
<div class="ad_fade" id="fade1">Title and Description details</div>
$('img').mouseenter(function(){
$(this).next('.ad_fade').show();
});
$('.ad_fade').mouseleave(function(){
$(this).hide();
});

Fade in information of ad when hovering unqiue div

I have many images each with their own unique id. each image is a link to its ad. I want to add a hover fadeIn effect that will display some info about that ad on top of the image.
I tried to do something but I don't think I am taking the right approach.
I feel like there is a better way to get the div on top rather than doing margin: -105px 0 0;
Also, I can't think of how to tell it "if id=1 is hovered, fadein id=fade1, else fadeout"
http://jsfiddle.net/mKDP4/4/
Dynamic div (in php)
<img src="test.jpg" class="ad_cover" id="1">
<div class="ad_fade" id="fade1"></div>
<img src="test.jpg" class="ad_cover" id="2">
<div class="ad_fade" id="fade2"></div>
<img src="test.jpg" class="ad_cover" id="3">
<div class="ad_fade" id="fade3"></div>
JQUERY
$('.ad_cover').mouseover(function() {
var ad_id = this.id;
$('#fade'+ad_id).fadeIn('slow');
});
$('.ad_cover').mouseout(function() {
var ad_id = this.id;
$('#fade'+ad_id).fadeOut('slow');
});
CSS
.ad_cover{
width:100px;
height:100px;
}
.ad_fade{
display:none;
position:absolute;
margin:-105px 0 0;
width:100px;
height:100px;
background:rgba(255,255,255,0.8);
}
All help is appreciated!
Instead of messing with margins in your CSS, you can use background-image and place the images inside of your div elements like so:
HTML
<div class="ad">
<img src="..."/>
</div>
<div class="ad">
<img src="..."/>
</div>
<div class="ad">
<img src="..."/>
</div>
CSS
.ad img {
width:100px;
height:100px;
display:none;
}
.ad {
width:100px;
height:100px;
background-image:url(...);
display: inline-block;
}
Then, using .hover() and .children(), you can hide/show the content of the divs like so:
$('.ad').hover(function() {
$(this).children('img').fadeIn('slow');
},
function() {
$(this).children('img').fadeOut('slow');
});
This way, you don't have to mess with combining IDs and classes and only have to use the one class on the parent divs.
Here is a fiddle of it in action: http://jsfiddle.net/9zP7g/

related width of two elements

I want to relate the width of two elements, the first is a simple div, the second is a span.
<div id="wb_Name_To_Menu" style="position:absolute;left:768px;top:20px;width:171px;height:18px;z-index:4;">
<span id="sp_Name_To_Menu" style="color:#FF0000;font-family:Arial;font-size:16px;">Love Me!</span></div>
<div id="Layer_to_mainu" style="visibility: hidden;position:absolute;text-align:left;left:772px;top:22px;width:132px;height:135px;z-index:132;" title="">
<div id="wb_main_mainu" style="position:absolute;left:8px;top:9px;width:123px;height:28px;z-index:1128;padding:0;">
<div id="main_mainu">
<ul style="display:none;">
<li><span></span><span id="id_full_name_from_main_menu">love Meeeeee</span>
<ul>
<li><span></span><span>Account Sitting</span></li>
<li><span></span><span>Help</span></li>
<li><span></span><span>Log Out</span></li>
</ul>
</li>
</ul>
</div>
in JavaScript:
$(document).ready(function(){
$("#wb_Name_To_Menu").hover(function(){
$("#Layer_to_mainu").css("visibility","visible");
$("#Layer_to_mainu").css("width",$("#id_full_name_from_main_menu").width()+"px");
});
});
$(document).ready(function(){
$("#Layer_to_mainu").mouseleave(function(){
$("#Layer_to_mainu").css("visibility","hidden");
});
});
it doesn't work! and I know the cause: it is caused by $("#id_full_name_from_main_menu").width() which results in a null value!
$("#id_full_name_from_main_menu").width() returns 0 because its ancestor <ul> has display:none set. To read the width you might want to just remove that attribute, or you could temporarily change it in the js, depending on what you're trying to do
$(document).ready(function(){
$("#wb_Name_To_Menu").hover(function(){
$("#Layer_to_mainu").css("visibility","visible");
$("#main_mainu > ul").css("display","block");
$("#Layer_to_mainu").css("width",$("#id_full_name_from_main_menu").width()+"px");
$("#main_mainu > ul").css("display","none");
});
That won't display your text but it will update the width of #id_full_name_from_main_menu,
you would need to remove the display:none to see your text appear.
If you remove +"px" On line 4 of the script you provided it seems to work.
$("#Layer_to_mainu").css("width",$("#id_full_name_from_main_menu").width());

How to define last-child when printing from a loop?

I use a PHP for-loop to print some portfolio items which echoes this:
echo "
<a href=\"portfoliodetail.php?id=$id\" class=\"noHover\" title=\"$title\">
<img src=\"images/portfolio/thumbnails/$bgthumbnail\" alt=\"$title\" />
</a>
";
However, I'm echoing images three times, and I want the last image to have a different margin than the others.
So I thought I'd just define a :last-child in the CSS, but when I put this margin at 0, all my margins are set at 0. Maybe when echoing this in a loop it thinks all the items are the :last-child or something? Is there a way to make the margin of the last image different?
Using CSS you can set the margin using the :last-child property
<style>
div a img{margin-left:10px;}
div a:last-child img{margin-left:50px;}
</style>
<div>
<img src="http://www.iwebsource.net/wp-content/uploads/2012/02/css3.png" />
<img src="http://www.iwebsource.net/wp-content/uploads/2012/02/css3.png" />
<img src="http://www.iwebsource.net/wp-content/uploads/2012/02/css3.png" />
</div>
FIDDLE EXAMPLE

Categories