PHP: Don't load images until div is expanded? - php

On my site, I have an updates section, which groups updates (galleries) into their own div. Each div is loaded hidden. They choose a section, and it expands to show the new galleries.
That all works fine. However, it loads all the images on the page load, which causes the hour glass to linger longer than I like. Is there any way to make an image load after a div is made visible?
Edit:
Went with:
$(function() {
$('.collapse img').attr('src', function(index, src) {
return this.getAttribute('data-src');
});
});
collapse is the div name that has the images in it. How can this be changed to only change the images in that current div? All the collapsed divs have the same name. However, when expanded, I want it to load only the images in that one div. Right now it loads all the images in all the collapsed div once I expand one of them.

Your divs should not contain the images at startup. You can load the images via JavaScript if a user expands your div.
Example:
function expandDiv(idOfDivElement) {
divObj = document.getElementById(idOfDivElement);
var imageObj = document.createElement('img');
imageObj.setAttribute('src', 'path/example_image.jpg');
divObj.appendChild(imageObj);
// your expand div code here
}
You could advance this function and add a second parameter for the image url. Or a array / comma seperated string if you want to load multiple images.

The process of loading images is up to the browser. You should load that content with javascript, like ajax.

You can load the images( within expandable div ) from ajax...
Just make the div visible, and then call ajax to get the images. Then use .innerHtml() or if you use jQuery, use .html() to put the images in the expanded div.
In this case you have to build the html in ajax page. It will just add the html to the eexpanded div.
This way loads the page much faster.
Please give feedback.

Related

HTML 5 multiple input file, pre loader with delete button

Hi could anyone help me
got this form that will render image when I select image, I would like to have a delete button each image just in case wrong image is selected.
got no idea how to do it.
any help, comments, links, suggestion will do. Thanks in advance.
this is my DEMO
//I just copy paste the code inside my demo.
Just clear the content inside #images_preview.
function clearContent()
{
document.getElementById('images_preview').innerHTML="";
}
Edited
Add below code just after you render the image (after document.getElementById('images_preview').appendChild(thumb);)
var element = document.createElement("input");
element.setAttribute("type", "button");
element.setAttribute("value", "clear");
element.addEventListener('click', clearContent, false);
document.getElementById('images_preview').appendChild(element);
In this code if you click the image a confirm box will ask you if you want to delete the image.
and removes it from DOM if you accept.
copy the addEventListener between the two lines of //your code
thumb.setAttribute('src',e.target.result);// your code
thumb.addEventListener('click',function(){
if(confirm('delete?')){
this.parentNode.removeChild(this)
}
},false);
document.getElementById('images_preview').appendChild(thumb); // your code
if you don't need to select the image for other purposes this is prolly the fastest way.
you also interact directly with the image with (this);
to create a nice button effect i would just add css style on hover.
#images_preview>img:hover:after{
//here u add a nice delete icon
}
if you need more buttons and the possibility to select each image i would put the image as background in a left floating div and inside the div put the different buttons you need.

Load first div from another page using jquery or php

I am not ever sure this is possible but I know you guys will know the answer.
I wonder if it is possible to load the first div on a page into a different page.
Example:
This code is on "list.php"
<div class="message_details">
Some Content 1
</div>
<div class="message_details">
Some Content 2
</div>
I found this article and it works but it brings in all div's but I want to load only the first div.
Load content from external page into another page using Ajax/jQuery
Here is the code I have now that loads all divs
$(document).ready(function() {
$(".loader").load('list.php .message_details');
});
How can I load only the first div? Also is there any way to remove an img tag within the selected div?
Thanks everyone
Use the :first-child selector
$(".loader").load('list.php .message_details:first-child', function() {
$(this).find("img").remove();
});
Reference
simply echo the the div in the second page. Give each div an id, then echo that. (unless i am misunderstanding your goal)

Use Jquery to prevent page reloading pressing anchor

I am trying to use the jquery to Prevent page realoading. I have the whole code php done... and is functional. if you can please go to http://www.jonathansconstruction.com/gallery3.php and click on the different gallery SUbmenus for category View All, Kitchen etc... i'm using get for variables. However, I would like to use a jquery to process the php so i dont have to include it into the gallery.php and also, to prevent page reloading because it brings the page back up and that could be confusing. Any hlep willl be greatly appreciated THanks
UPDATE, Thanks for everyone that helped:
SO far i made an advance on the website, Corrected URL was edited on top of the post
Everything is working smoothly on the effects of quicksand.. HOwever, Lytebox Doesn't Load when a quicksand effect is placed. , at the beginning it loads just fine, but right after pressing one of the menus for the quicksand effect. It stops working. Also, I want to style my menu buttons as is in http://www.jonathansconstruction.com/gallery.php. I see jquery doesnt add and witht the sample I downloaded. and also the that says "gallery Pictures" dissapears ont he quicksand demo.
Any Help is appreciated. Below is the script.js code I have modified so far
$(document).ready(function(){
var items = $('.photo_show figure'),
itemsByTags = {};
// Looping though all the li items:
items.each(function(i){
var elem = $(this),
tags = elem.data('tags').split(',');
// Adding a data-id attribute. Required by the Quicksand plugin:
elem.attr('data-id',i);
$.each(tags,function(key,value){
// Removing extra whitespace:
value = $.trim(value);
if(!(value in itemsByTags)){
// Create an empty array to hold this item:
itemsByTags[value] = [];
}
// Each item is added to one array per tag:
itemsByTags[value].push(elem);
});
});
// Creating the "Everything" option in the menu:
createList('View All',items);
// Looping though the arrays in itemsByTags:
$.each(itemsByTags,function(k,v){
createList(k,v);
});
$('#gallery_menu nav a').live('click',function(e){
var link = $(this);
link.addClass('current').siblings().removeClass('current');
// Using the Quicksand plugin to animate the li items.
// It uses data('list') defined by our createList function:
$('.photo_show').quicksand(link.data('list').find('figure'), {adjustHeight: 'dynamic'} );
e.preventDefault();
});
$('#gallery_menu nav a:first').click();
function createList(text,items){
// This is a helper function that takes the
// text of a menu button and array of li items
// Creating an empty unordered list:
var ul = $('<ul>',{'class':'hidden'});
$.each(items,function(){
// Creating a copy of each li item
// and adding it to the list:
$(this).clone().appendTo(ul);
});
ul.appendTo('.photo_show');
// Creating a menu item. The unordered list is added
// as a data parameter (available via .data('list'):
var a = $('<a>',{
html: text,
href:'#',
data: {list:ul}
}).appendTo('#gallery_menu nav');
}
});
ANOTHER EDIT :
All Looking good So far Fixed Lot of problems, However, for some reason, the span I had on teh static html dissapears from display and even html code when loading jquery quicksand code..?? Here is the code i have on the html part of the website that Does Not appear on the live website for some reason.
<div id="portfolio">
<div class="photo_show"><span>Gallery Pictures</span>
the span part doesnt appear, dont know why
I had the TOp Part Resolvd. just moved the on top of photo_show div and edited positioning... HOPEFULLY Last Edit
the jquery for quicksand made my calendar dissapear, checked and yes it was some jquery conflict but dont know what can be causing it.. also the form validation not working as well ... any help is appreciated!
I visited your webpage link after correcting a typo in the URL for word construction.
I also see the problem that the page reloads when clicking on a sorting filter such as View All, Kitchen, and Miscellaneous which is what you want to prevent.
Unfortunately, those buttons are using URL Links asking to reload the webpage with a filtered option via query string. Nothing can be done for that method of filtering, your just going to reload the webpage since filtering is done on page load.
Perhaps this DEMO shows what you want to accomplish since it does not reload the webpage. That demo has markup that is easy to create and maintain which is build using the Quicksand jQuery Plugin. The tutorial for that demo is accessed using the link at the bottom right, but looking at the source HTML file shows how simple it is.
I made a downloadable demo using both Quicksand and Shadowbox, a lightbox alternative HERE, which might interest you since your webpage is linking the filtered icon results to a lightbox alternative named Lytebox.
Since your edit reflects your interest in Lytebox, the following markup will reinitialize that lightbox alternative after a filtering event has occurred with Quicksand. Update your script.js file accordingly.
$('.photo_show').quicksand(link.data('list').find('figure'), function(){
adjustHeight = 'dynamic';
initLytebox();
});
I also made a correction to your existing adjustHeight since it was using a semicolon instead of an equal sign, but the correct location and use of that is for something unrelated.
The reason that you are not seeing the bullets is because your not using ul/li tags that the demo is using.

jQuery fancy box question

I have a jQuery fancybox with an iframe. It loads the code from a .php.
Inside that .php file, I have a button which does a post when clicked.
$(".next").click(function() {
$.post("update.php", {page: $(this).attr('data-page')}, function(success){
$("#dialog").html(success);
}
);
});
The issue is that when I click this for the first time it loads up the html, however it does not work the second time. It seems that jQuery is not loaded for the second time as I am using the star-rating and the star-rating is not rendered there as well.
Initially my dialog has some html code in it.
To illustrate, here's how the structure of my page. I have a main.php. I have a button inside main.php that when I click launches an iframe fancy box. The content of this fancy box is loaded from a .php file, called content.php. Inside the content.php I have another button that when clicked does a post to loader.php. loader.php echoes a bunch of html. The jQuery code to post is located at content.php When I click on the button inside the iframe for the second time, it doesn't do anything
Well, Fancybox is really just loading a div over your current content, so if the content of that div (the Fancybox "dialog") changes after the DOM is set (in other words, the page is done loading) then the events are not attached to the new content by jQuery. You'd have to modify your code to use the .live() function.
Instead of click, you have to use live. Change first line as:
$(".next").live('click', function() {

jquery hide problem

I use this to toggle my div elements, and hide all them when the DOM is ready...
$('div[class*="showhide"]').hide();
$('input:image').click( function() {
var nr = $(this).attr('id').substr(7,2);
$('div.showhide' + nr).toggle(400);
});
I have dynamically created div elements with class showhide0;showhide1;showhide2...etc...
Inside the DIV tags I have search boxes.
First when page is loaded all DIV tags hide.
I toggle one of them to show.
Start a search, so the page is reloaded with the result of the query.
Of course all DIV is hide again, because the page is reloaded. Unfortunately...
Is it possible to not hide again after I searched for something? It would be nice when I open the page, all the divs are hidden, but after then just when I toggle it...
If you need a specific element or elements to stay visible upon a page reload, then you're going to need to do something to maintain state across requests, and then modify your jQuery to utilize that state information when initializing the visible state of the elements.
This can be done in numerous ways which include but are not necessarily limited to
Include it in the query string
Include it in the URL hash
Use a cookie
Well, yeah, you just don't run the initial hide() if there's a search request. I'd just exclude that line from the output if, on the PHP level, you know you're executing a search.
We do something similar to this where I work.
We opted instead of have the class name just be hide for all elements and instead have the ids named.
So, we'd have it something like:
<div id="hide1" class="hide"> </div>
along with this CSS to hide all those divs by default
.hide {
display: none;
}
Finally, we use something like this to show them:
$('input:image').click( function() {
var nr = $(this).attr('id').substr(7,2);
$('#hide' + nr).toggle(400);
});
}
This works because of CSS precedence rules. The toggle()/hide()/show() method overrides the hide class's style.
As for the unhiding part, if you pass the ID to unhide to your script, you can parse it and unhide the appropriate div.
You can read and process the query string from window.location.search. Unfortunately, you then have to manually parse it or use a plugin, such as jQuery Query String Object or jQuery URL Utils.
var id = $.query.get('unhide_id'); // This is using Query String Object
$('#' + id).show(400);

Categories