I have page with image loading Dynamical from php.The image height is
various px size(70,130,100,60).my need is how can get the image size currently viewed in page automatically in alertbox i try same code is not working.please help
code
<?php
foreach ($images as $image) {
?>
<img src="<?php echo $image['url']; ?>" class="img_test" id="<?php echo $image['url']; ?>" width="70" style="max-height:70px" onclick="imagesize(this)" />
<?php
}?>
<script>
$( window ).load(function() {
$( ".img_test" ).each(function( index ) {
alert('Height: '+$(this).height());
});
});
function imagesize(obj){
alert($(obj).height());
}
</script>
if we click the image the height will display.in page load after automatically can't be display
It's important to understand that it's navigator (client) which load image and PHP is running into your server and send text to client.
Use a listener .on to get event 'click'.
Moreover, use $(document).ready and not .load to wait that images are loading
<?php foreach ($images as $image) { ?>
<img src="<?php echo $image['url']; ?>" class="img_test" id="<?php echo $image['url']; ?>" width="70" style="max-height:70px" />
<?php } ?>
$(document).ready(function(){
$( ".img_test" ).each(function(){
alert('Height: '+$(this).height());
});
$(".img_test").on("click", function(){
alert($(this).height());
});
});
http://jsfiddle.net/3c9CN/
Related
I successfully implemented a Masonry-Gallery for a WordPress website by using Bootstrap 5 in combination with 'Masonry-Layout'.
It works all fine except in Safari, where the layout usually breaks, a known problem and the solution is supposed to be 'imagesLoaded'.
I tried a lot, but I just don't manage to add the needed classes at the right place(s).
The code as far as I got, see below. Anybody who can help?
<div class="row grid" id="gallery-masonry" data-masonry='{"percentPosition": true }'>
<?php
$images = get_field('gallery');
$size = 'large';
if( $images ): ?>
<?php foreach( $images as $image_id ): ?>
<div class="col-md-6 mb-3">
<div id="post-<?php the_ID(); ?>" class="card rounded-0" >
<?php echo wp_get_attachment_image( $image_id, $size, "", ["class" => "card-img-top rounded-0","alt"=>""]); ?>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<!--Masonry-Layout-->
<script src="https://cdn.jsdelivr.net/npm/masonry-layout#4.2.2/dist/masonry.pkgd.min.js" integrity="sha384-GNFwBvfVxBkLMJpYMOABq3c+d3KnQxudP/mGPkzpZSTYykLBNsZEnG2D9G/X/+7D" crossorigin="anonymous" async></script>
<!--ImagesLoaded-->
<script src="https://unpkg.com/imagesloaded#5/imagesloaded.pkgd.min.js"></script>
<!--Script ImagesLoaded-->
<script>
var $grid = $('.grid').imagesLoaded( function() {
// init Masonry after all images have loaded
$grid.masonry({
// options...
});
});
</script>
Please check my post regarding adding a Masonry to a Wordpress Site.
https://www.customfitwebdesign.com/how-to-add-masonry-layout-in-wordpress/
Note: Masonry is actually built into the core of WordPress ( wp-includes/js/masonry.min.js ). This WordPress version of Masonry also includes ImagesLoaded (imagesloaded.min.js).
I am trying to position a div vertically in the middle of a window in Wordpress.
I have a function.js file with the following code:
function setToCenter()
{
var a = document.getElementById("R_center");
var h = window.innerHeight;
var p = (h/2)-(650/2);
if( p < 80 ){ p = 80; }//limit to header height
//a.style.transform = "transform(0,"+p+"px)";
//a.style.margin-top= p+"px";
a.style.paddingTop = p+"px";
}
and a php file to get the post content:
<?php
/*
Template Name: Page R Center
*/
?>
<?php global $optimizer;?>
<?php get_header(); ?>
<div id="R_center" class="R">
<script type="text/javascript"> setToCenter(); </script>
<?php if(have_posts()): ?><?php while(have_posts()): ?><?php the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<div class="thn_post_wrap">
<?php the_content(); ?>
</div>
</div>
<?php endwhile ?>
</div><!--R_center class END-->
<?php endif ?>
<?php get_footer(); ?>
and in my header.php:
<script type="text/javascript" src="http://localhost/wp/wp-content/themes/r-child/assets/js/functions.js"></script>
evrything is working well. The Div is positioned in the vertical middle of my screen. BUT because the Div is rendered after the java script it works only after refreshing the page.
My question is where do I have to place my javascript to avoid refreshing? Can I execute everything from my functions.js without calling function in my php?
thx
In your case you need to launch setToCenter function after DOM is loaded by browser. You should wrap it into DOMContentLoaded event, example:
document.addEventListener("DOMContentLoaded", function(event) {
setToCenter();
});
or if you use jQuery:
$(function(){
setToCenter();
});
The easiest way is to place your function call near the footer even you can place it after the footer as well. this will solve you problem.
You have asked "can you place the code in function.js?" yes you can do this as well. just place function call inside window.load function and put this code in function.js file.
Below are the example for both cases .
$(window).load(function() { setToCenter(); });
OR
<?php
/*
Template Name: Page R Center
*/
?>
<?php global $optimizer;?>
<?php get_header(); ?>
<div id="R_center" class="R">
<?php if(have_posts()): ?><?php while(have_posts()): ?><?php the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<div class="thn_post_wrap">
<?php the_content(); ?>
</div>
</div>
<?php endwhile ?>
</div><!--R_center class END-->
<?php endif ?>
<?php get_footer(); ?>
<script type="text/javascript"> setToCenter(); </script>
I wonder if anyone could help me with a problem I am having with bxslider. I am creating a Wordpress site and using the jQuery bxslider instead of the plugin version. Everything with the slider is fine when you load the website, but when you leave the homepage to go to another page, and then come back the slider is broken.
When I say broken, both slides are showing and the JavaScript has stopped. Im using Bootstrap 3 as the framework of the theme.
I'm not used to posting on here, so sorry if I have made a mistake please feel free to correct it.
Any help would be appreciated.
Many Thanks
<div class="container">
<section class="row" id="slidercontainer">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" id="sliderholder">
<ul class="bxslider">
<?php
global $post;
$args = array('numberposts' => 4, 'category' => 2, 'post_status'=>"publish");
$myposts = get_posts($args);
foreach($myposts as $post) : setup_postdata($post);
?>
<li id="post-<?php the_ID(); ?>" class="slider-item">
<h1><?php echo get_the_title();?></h1>
<?php
$imageArray = get_field('slider_image'); // Array returned by Advanced Custom Fields
$imageAlt = $imageArray['alt']; // Grab, from the array, the 'alt'
$imageThumbURL = $imageArray['sizes']['postslider']; //grab from the array, the 'sizes', and from it, the 'thumbnail'
?>
<img class="pull-right" src="<?php echo $imageThumbURL;?>" alt="<?php echo $imageAlt; ?>">
<?php
$content = apply_filters( 'the_excerpt', get_the_excerpt() );
$content = str_replace( ']]>', ']]>', $content );
?>
<div class="slider-content"><?php echo $content;?></div>
</li>
<?php
endforeach;
wp_reset_postdata();
?>
</ul>
</div>
</section>
</div>
<!-- JavaScript -->
$(document).ready(function(){
$('.bxslider').bxSlider({
maxSlides: 4,
pager: false,
auto: true,
autoHidePager: true,
pause: 10500,
controls: true,
speed: 1800,
mode: 'horizontal'
});
});
The variable in $pieces[12] is the url of an image on my server, some times when the page loads there is a broken image. My script is suppose to find the broken image error if there is one and replace it with $pieces[12] again. If that sounds right. But I don't know how to do it with my code.
<div class="right_ad" id="right_ad">
<script>
<img src="<?php echo $pieces[12]; ?>"
onError="this.onerror=null;this.src='<?php echo $pieces[12]; ?>';"
style="width: 100%;max-height: 100%"/>
</script>
</div>
You should urlencode those values. My tip is that there are special values in your filenames.
function onError_run_this_func(){
//get your new image url with ajax function
//or if you already have it in $pieces[12] then use it.
$.('.image-class).attr('src', '<?php echo $pieces[12]; ?>');
}
<div class="right_ad" id="right_ad"><script><img class="image-class" src="`<?php echo $pieces[12]; ?>" onError="onError_run_this_func()" style="width: 100%;max-height: 100%"/></script> </div>`
I am new to this and I am struggling with one thing for such a long time...
I use foreach loop to show all images in a list, that works fine.
<?php
foreach($listImages as $image) {
?>
<a href="#" class="show_hide" title="<?php echo $image; ?>" >
<img src="foto.php?file=<?php echo $image; ?>" alt="building thumb"/>
</a>
<?php
}
Then I use simple show and hide script that looks like this:
<script type="text/javascript">
$(document).ready(function(){
$(".slidingDiv").hide();
$(".show_hide").show();
$('.show_hide').click(function(){
$(".slidingDiv").slideToggle();
});
});
</script>
Finally, when I use div slidingDiv, it works as I expect, but I also need to know name of the image I clicked on but I really don't know how to do that.
<div class="slidingDiv">
!!! I need to know $image from the foreach loop here !!!
</div>
Thank you for your help!