Lazy loading of images for website to load faster - javascript

i want that before the actual image gets loaded a spinner should be shown so that my website can load faster
window.addEventListener('load', function(){
var allimages= document.getElementsByTagName('img');
for (var i=0; i<allimages.length; i++) {
if (allimages[i].getAttribute('data-src')) {
allimages[i].setAttribute('src', allimages[i].getAttribute('data-src'));
}
}
}, false)
<img src="https://loremflickr.com/400/600" data-src="images/banguet-hall-location-icon.png" class="secEightImg" />
<img src="http://www.jettools.com/images/animated_spinner.gif" data-src="images/banguet-hall-location-icon.png" class="secEightImg" />
<script>
</script>
the problem is the actual image is shown two times and instead of spinner a broken image (which is shown when no image is found) is shown first
please help

If you want a spinner per image, just add the spinner URL as the initial src attribute for all of them:
<img src="spinner.gif" data-src="actual-image.png" />
Then, once the page loads, change all those src for the real URL (data-src) and listen for the load and error events on each image.
For each of them, add a .loaded class or .error class to the images and style them as you want. For example, you could hide the ones that could not be loaded, show a custom "error" image (using background-image) or style them as you wish, like in this example:
function imageLoaded(e) {
updateImage(e.target, 'loaded');
}
function imageError(e) {
updateImage(e.target, 'error');
}
function updateImage(img, classname) {
// Add the right class:
img.classList.add(classname);
// Remove the data-src attribute:
img.removeAttribute('data-src');
// Remove both listeners:
img.removeEventListener('load', imageLoaded);
img.removeEventListener('error', imageError);
}
window.addEventListener('load', () => {
Array.from(document.getElementsByTagName('img')).forEach(img => {
const src = img.getAttribute('data-src');
if (src) {
// Listen for both events:
img.addEventListener('load', imageLoaded);
img.addEventListener('error', imageError);
// Just to simulate a slow network:
setTimeout(() => {
img.setAttribute('src', src);
}, 2000 + Math.random() * 2000);
}
});
})
html,
body {
margin: 0;
height: 100%;
}
.images {
width: 100%;
height: 100%;
background: #000;
display: flex;
align-items: center;
overflow-x: scroll;
}
.margin-fix {
border-right: 1px solid transparent;
height: 16px;
}
img {
width: 16px;
height: 16px;
margin: 0 64px;
}
img.loaded {
width: auto;
height: 100%;
margin: 0;
}
img.error {
background: red;
border-radius: 100%;
/* You could add a custom "error" image here using background-image */
}
<div class="images">
<img
src="https://i.stack.imgur.com/RvfGz.gif"
data-src="https://d39a3h63xew422.cloudfront.net/wp-content/uploads/2014/07/20145029/driven-by-design-the-incomparable-lancia-stratos-1476934711918-1000x573.jpg" />
<img
src="https://i.stack.imgur.com/RvfGz.gif"
data-src="https://car-images.bauersecure.com/pagefiles/76591/1752x1168/ford_racing_puma_01.jpg?mode=max&quality=90&scale=down" />
<img
src="https://i.stack.imgur.com/RvfGz.gif"
data-src="http://doesntexist.com/image.jpg" />
<span class="margin-fix"></span>
</div>
The problem is that the spinner image you use initially might take some time to load.
One solution would be to use a data URI, so instead of:
<img src="https://i.stack.imgur.com/RvfGz.gif" data-src="actual-image.png" />
You would have:
<img src="data:image/gif;base64,R0lGODlhEAAQAPYAAAAAAP///yoqKmpqap6enr6+vrq6upCQkFxcXCIiIlpaWtra2tbW1s7OzsjIyMDAwJSUlEREROLi4oyMjBISEhAQEDw8PHR0dK6urqCgoEBAQC4uLsTExOjo6HJyclRUVKKiooKCghwcHHh4ePDw8JaWlmJiYpiYmEhISLi4uPT09E5OTmhoaObm5vj4+BYWFgoKCoaGhnp6eggICHx8fFZWVgQEBAICAj4+PjQ0NAYGBigoKFBQUA4ODiwsLBoaGiAgIDAwMDg4OEJCQh4eHiYmJgwMDCQkJISEhEpKSkxMTLKysqysrKSkpJycnLy8vMLCwjo6OoiIiMzMzBQUFNTU1HBwcKamptLS0uDg4F5eXrCwsOzs7HZ2dpqamsrKyjY2NjIyMhgYGEZGRoCAgGxsbGBgYKioqG5ubrS0tLa2ttzc3FhYWO7u7vLy8lJSUvr6+mRkZNjY2Orq6sbGxoqKitDQ0Pb29o6Ojt7e3qqqqpKSkn5+fgAAAAAAAAAAACH+GkNyZWF0ZWQgd2l0aCBhamF4bG9hZC5pbmZvACH5BAAKAAAAIf8LTkVUU0NBUEUyLjADAQAAACwAAAAAEAAQAAAHjYAAgoOEhYUbIykthoUIHCQqLoI2OjeFCgsdJSsvgjcwPTaDAgYSHoY2FBSWAAMLE4wAPT89ggQ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nJhaXJpZXMvY2xhc3MubXlzcWwucGhwPC9iPiBvbiBsaW5lIDxiPjY4PC9iPjxiciAvPgo8YnIgLz4KPGI+V2FybmluZzwvYj46ICBteXNxbF9xdWVyeSgpIFs8YSBocmVmPSdmdW5jdGlvbi5teXNxbC1xdWVyeSc+ZnVuY3Rpb24ubXlzcWwtcXVlcnk8L2E+XTogQSBsaW5rIHRvIHRoZSBzZXJ2ZXIgY291bGQgbm90IGJlIGVzdGFibGlzaGVkIGluIDxiPi9ob21lL2FqYXhsb2FkL3d3dy9saWJyYWlyaWVzL2NsYXNzLm15c3FsLnBocDwvYj4gb24gbGluZSA8Yj42ODwvYj48YnIgLz4K" data-src="actual-image.png" />
As you can see, your HTML document will grow fast using this approach, which is a problem.
A better approach might be to use CSS to add the spinner so that you only include the data URI once. To do that, you need to add an empty src attribute to your images initially anyway:
<img src data-src="actual-image.png" />
Or:
<img src="" data-src="actual-image.png" />
If you don't put it, the image will have an annoying gray border you can't get rid of until you add a src attribute.
function imageLoaded(e) {
updateImage(e.target, 'loaded');
}
function imageError(e) {
updateImage(e.target, 'error');
}
function updateImage(img, classname) {
// Add the right class:
img.classList.add(classname);
// Remove the data-src attribute:
img.removeAttribute('data-src');
// Remove both listeners:
img.removeEventListener('load', imageLoaded);
img.removeEventListener('error', imageError);
}
window.addEventListener('load', () => {
Array.from(document.getElementsByTagName('img')).forEach(img => {
const src = img.getAttribute('data-src');
if (src) {
// Listen for both events:
img.addEventListener('load', imageLoaded);
img.addEventListener('error', imageError);
// Just to simulate a slow network:
setTimeout(() => {
img.setAttribute('src', src);
}, 2000 + Math.random() * 2000);
}
});
})
html,
body {
margin: 0;
height: 100%;
}
.images {
width: 100%;
height: 100%;
background: #000;
display: flex;
align-items: center;
overflow-x: scroll;
}
.margin-fix {
border-right: 1px solid transparent;
height: 16px;
}
img {
width: 16px;
height: 16px;
margin: 0 64px;
background-image: url("data:image/gif;base64,R0lGODlhEAAQAPYAAAAAAP///yoqKmpqap6enr6+vr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
/* You could add a custom "error" image here using background-image */
}
<div class="images">
<img
src
data-src="https://d39a3h63xew422.cloudfront.net/wp-content/uploads/2014/07/20145029/driven-by-design-the-incomparable-lancia-stratos-1476934711918-1000x573.jpg" />
<img
src
data-src="https://car-images.bauersecure.com/pagefiles/76591/1752x1168/ford_racing_puma_01.jpg?mode=max&quality=90&scale=down" />
<img
src
data-src="http://doesntexist.com/image.jpg" />
<span class="margin-fix"></span>
</div>
Other approaches might be to just hide the images initially with display: none for example, in which case you might want a wrapper around them to show something like an empty box to indicate the users that something will be shown in there, or even a spinner made with other elements if you want/need to get fancy.

What you can do is something like:
$(".sly-main-slider div img").attr('src', "#");
$(".sly-main-slider").addClass("loading");
$(".sly-main-slider div").hide();
$(".sly-main-slider div img").load(function() {
$(".sly-main-slider div").show();
$(".sly-main-slider").removeClass("loading");
}).attr('src', "https://loremflickr.com/400/600");
.loading {
position: absolute;
top: 10%;
left: 10%;
z-index: 2000;
background: url(http://1.bp.blogspot.com/-nfXo9GWbDtM/VOn0vr4yLMI/AAAAAAAABCA/dDNgd7_QCFo/s1600/block-loader.gif) no-repeat center center;
height: 32px;
width: 32px;
}
.loading:after {
position: absolute;
top: 8px;
left: 40px;
content: 'Loading...';
}
body {
position: relative;
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='sly-main-slider'>
<div>
<img src="https://loremflickr.com/400/600" alt="" />
</div>
</div>

Related

Updating SRC attribute of dynamically loaded image with jQuery

I am trying to dynamically load an image tag, and on the very next line, trying to change its SRC attribute. It is not working. Could it be because it does not allow enough 'time' for the image tag to load into a receptacle DIV (#Div_Extend_2), before its SRC attribute get changed? The whole image upload form is loaded dynamically, and if the image tag is included in the first dynamic inclusion, it works; I am able to change the SRC atribute of the image. But if the img tag is put there using load(), just before the change attribute statement: $("#IMG_wlpst_PREV").attr('src',"IMAGES/Apples_in_a_basket.JPG"); is executed, it doesn't work.
Please note, in the CSS I have put display: none; for the image, when its included as part of the POST_IMG_1.PHP file.
Thank you, all help appreciated!! Please stay safe in this coronavirus period..
The main HTML document is as follows:
<div id='DIV_MAINPOST'>
<div id="DV_post_type">
<div id="div_POST_TXT">TEXT</div>
<div id="div_POST_IMG">Image!</div>
</div>
<div id="DIV_ERROR"> </div>
<div id="Div_Extend_1"> </div>
</div>
Javascript:
$('#DIV_MAINPOST').click(function(e){
switch(e.target.id)
{ case "div_POST_TXT":
$('#Div_Extend_1').html("");
break;
case "div_POST_IMG":
$('#Div_Extend_1').load('POST_IMAGE_1.php');
break;
} });
$('#DIV_MAINPOST').on('submit','#FORM_img', function(e){
alert("formsubmit");
e.preventDefault();
var FD = new FormData(this);
var FIL = FD.get("file_");
var ERS = 0;
if(FIL.length===0)
{ ERS++;
$("#DIV_ERROR").html("Error: No file");
}else if(!((FIL.type=='image/jpeg' ) || (FIL.type=='image/jpg') || (FIL.type=='image/png') || (FIL.type=='image/bmp')))
{ ERS++;
$("#DIV_ERROR").html("Err: Not Image");
}else if(FIL.size > (6*1024*1024)) //> 4MB
{ ERS++;
$("#DIV_ERROR").html("Err: Too Large");
}
if(ERS==0)
{ $("#IMG_wlpst_PREV").show(); //case: 1 tag is present with POST_IMG_1.php, default css is display: none;
$("#Div_Extend_2").load('POST_IMAGE_2.php'); // case: 2 loading tag with this stmt
$("#IMG_wlpst_PREV").attr('src',"IMAGES/Apples_in_a_basket.JPG"); //updating its info
} });
});
POST_IMG_1.PHP
<FORM method='post' action='PHP/FILE_UPLOAD_CHECK.PHP' id="FORM_img" enctype='multipart/form-data'>
<INPUT type='file' name='file_' size='10'></INPUT>
<button type='submit' value='upload'>Upload</button>
</FORM>
<div id="Div_Extend_2">
<img id="IMG_wlpst_PREV"></img> //case 1:: img tag included in the same file (which is also load()'ed, works fine. SRC gets updated.
//case 2:: this tag is not present, but the "Div_Extend_2" div is still present in POST_IMG_1.php.
</div>
POST_IMG_2.PHP
<img id="IMG_wlpst_PREV"></img> //case 2::if loaded using $("#Div_Extend_2").load('POST_IMG_2.PHP'), doesn't work;
CSS
#DIV_MAINPOST
{ width: 310px;
background-color: grey;
display: flex;
flex-direction: column;
}
#DV_post_type
{ order: 1;
flex-direction: row;
display: flex;
}
#div_POST_TXT
{ order: 1;
width: 150px;
border: 2px solid black;
background-color: pink;
}
#div_POST_IMG
{ order: 2;
width: 150px;
border: 2px solid black;
background-color: pink;
}
#DIV_ERROR
{ order: 2;
border: 1px solid red;
width: 300px;
height: 30px;
color: red;
}
#Div_Extend_1
{ order: 3;
}
#IMG_wlpst_PREV
{ display: none; //case 1
width: 300px;
height: 300px;
}

Jquery: show IMG on click

Preview of how it should be: https://www.youtube.com/embed/8qKRf0cg3Co
As in the video, I have to click on the img to get a large view of the img under it.
So far this is my code:
HTML:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>JQUERY</title>
<script src="jquery/jquery-2.1.1.min.js"></script></head>
<body>
<center>
<div id="wrapper">
<div id="nav">
<div id="overbalk"></div>
<img src="images/foto_01.png" align="top" class="foto" />
<img src="images/foto_02.png" align="top" class="foto" />
<img src="images/foto_03.png" align="top" class="foto" />
<img src="images/foto_04.png" align="top" class="foto" />
</div>
<div id="content">
<img src="images/foto_01.png" align="top" class="slide_foto" />
</div>
</div>
</center>
</body>
</html>
CSS:
<style>
body {
padding: 0;
margin: 0;
background-color: black;
}
#nav {
width: 600px;
margin: 0px auto;
}
#overbalk {
width: 600px;
height: 30px;
position: absolute;
background-color: black;
}
.foto {
width: 75px;
height: 75px;
margin-top: -20px;
border: 1px solid black;
}
.foto:hover {
cursor: pointer;
}
#content {
position: absolute;
top: 50px;
z-index: -2;
width: 100%;
}
.slide_foto {
margin-left:-3200px;
}
</style>
JS:
$(document).ready(function (){
$('.foto').mouseenter(function () {
$(this).animate({marginTop: '+=50'}, 200);
});
$('.foto').mouseleave(function (){
$(this).animate({marginTop: '-=50'}, 200);
});
$('.foto').click(function () {
$(this).next('#content').show();
});
});
I also tried this:
$('.foto').on('click', function () {
$('#content').html(nieuwe_foto).show();
var nieuwe_foto = $(this).attr('src');
$('.slide_foto').attr('src', nieuwe_foto);
}
None of this worked, and got a little bit stuck, the images aren't showing below the small images.
You need to make 2 changes:
Remove this style class from <style>
.slide_foto {
margin-left:-3200px;
}
Make this change in your onclick handler
$('.foto').on('click', function () {
var nieuwe_foto = $(this).prop('src');
$('.slide_foto').prop('src', nieuwe_foto);
});
I have made a working fiddle with sample images https://jsfiddle.net/sachin_puthran/c2qgjpj1/
The following doesn't do anything since nieuwe_foto is undefined until the next line and div#content is already visible .show() doesn't do anything either.
$('#content').html(nieuwe_foto).show();
The .show() isn't what you're looking for. It will essentially "unhide" an element. So if an element has a display: none style then .show() will restore the initial display style. See the docs.
You're closer with your second attempt though. All you want to do in the $('.foto').click function is set the src of the .slide_foto element to what is currently in the src of the this object.

Display selected picture under it's button with multiple buttons

Sorry the title is not better. Here is where I'm at. I am trying to open an openFileDialog, then display the image I picked below that button. I can do it fine with one button and one image. However I can't seem to get the selected picture to show under only the appropriate button. My script looks like this.
$('.custom-upload input[type=file]').change(function() {
var parentCommentId = $(this).id
$(this).next().find('input').val($(this).val());
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
}
function imageIsLoaded(e) {
$('img').attr('src', e.target.result);
$('img').fadeIn();
};
});
.custom-upload {
position: relative;
height: 40px;
width: 350px;
margin: 30px;
}
.custom-upload input[type=file] {
outline: none;
position: relative;
text-align: right;
-moz-opacity: 0;
filter: alpha(opacity: 0);
opacity: 0;
z-index: 2;
width: 100%;
height: 100%;
}
.custom-upload .fake-file {
background: url(http://www.fold3.com/i/upload-icon.png) center right no-repeat;
position: absolute;
top: 0px;
left: 0px;
width: 350px;
padding: 0;
margin: 0;
z-index: 1;
line-height: 100%;
}
.custom-upload .fake-file input {
font-size: 16px;
height: 40px;
width: 300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="custom-upload" id="taco">
<input type="file" id="eatme">
<div class="fake-file">
<input disabled="disabled" id="damn">
</div>
<img id="myImg" src="#" style="display: none;height:400px;" width=100% height=100%/>
</div>
<br>
<div id="result"></div>
<div class="custom-upload">
<input type="file">
<div class="fake-file">
<input disabled="disabled">
</div>
<img id="myImg" src="#" alt="your image" style="display: none;" width=100% />
</div>
Using $("img") selects all img elements. You need to use DOM navigation methods to get from the input for which the change event occurred to its related img element.
$('.custom-upload input[type=file]').change(function() {
var $this = $(this); // store a reference to current input
$this.next().find('input').val($(this).val());
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
}
function imageIsLoaded(e) {
$this.closest('.custom-upload')
.find('img')
.attr('src', e.target.result)
.fadeIn();
};
});
I've saved a reference to the current file input in the (new) variable $this. Then in the img onload handler I've used .closest() to navigate up to the containing div and then .find() to go back down to the img in that div.
(You could use $this.siblings("img") or $this.next().next(), but that would only work for your specific html structure as it is right now - using .closest() plus .find() will work even if you later add an extra (sub-)container div around the img or otherwise tweak the structure, just as long as the file input and img are still within the same containing div.)
Demo: http://jsfiddle.net/jndL3rj5/

Dynamic JavaScript Injection/Execution

A small little rundown of what I'm trying to do:
1) Dynamically determine if a browser is capable of playing HTML5 video (Working)
2) If not capable of playing HTML5 video, load Flash version of video, CSS, and page (Working)
3) If capable of playing HTML5 video, load HTML5 version of video, CSS, and page (Stuck)
All the code needed loads into the browser (IE10, IE11, and Chrome 36 have been tested). The problem is with the execution of JavaScript that is added through the dynamic loading. I'm using the Adobe Captivate CS6 publishing system for Flash and HTML5 output, and all my customized code works perfectly if the scripts are loaded normally, but not through this dynamic method. To save a little bit of effort, I'm just going to post the code for the HTML5 version.
function addExternalStyleSheet(href)
{
if(document.createStyleSheet)
{
document.createStyleSheet(href);
}
else
{
var newSheet = document.createElement('link');
newSheet.setAttribute('rel', 'stylesheet');
newSheet.setAttribute('type', 'text/css');
newSheet.setAttribute('href', href);
document.getElementsByTagName('head')[0].appendChild(newSheet);
}
}
function addInternalStyleSheet(selector, attributes)
{
if(document.createStyleSheet)
{
var styleSheet = document.createStyleSheet('');
styleSheet.cssText = selector + '{' + attibutes + '}';
}
else
{
var styleTag = document.createElement('style');
var head = document.getElementsByTagName('head')[0];
head.appendChild(styleTag);
var sheet = styleTag.sheet ? styleTag.sheet : styleTag.styleSheet;
if(sheet.insertRule)
sheet.insertRule(selector + '{' + attributes + '}', 0);
else
sheet.addRule(selector, attributes, 0);
}
}
function addJavaScript(src)
{
var newScript = document.createElement('script');
newScript.setAttribute('type', 'text/javascript');
newScript.setAttribute('src', src);
document.getElementsByTagName('head')[0].appendChild(newScript);
}
function generateEmbed()
{
if(fileType == 'html5')
{
addExternalStyleSheet('assets/playbar/playbarStyle.css');
addExternalStyleSheet('assets/css/CPLibrary.css');
addExternalStyleSheet('assets/css/CPQuiz.css');
addExternalStyleSheet('assets/css/smoothness/jquery-ui-1.8.14.custom.css');
addExternalStyleSheet('assets/css/dd.css');
addInternalStyleSheet('.shadow', 'overflow: hidden;');
addInternalStyleSheet('.cpMainContainer', 'background-color: transparent; height: 100%; width: 100%; padding: 0px; position: absolute;');
addInternalStyleSheet('.blocker', 'position: absolute; left: 0px; top: 0px; display: none;');
addInternalStyleSheet('.loadingBackground', 'background-color: #777777; opacity: 0.5;');
addInternalStyleSheet('.loadingString', 'color: #ffffff;');
addJavaScript('assets/js/jquery-1.6.1.min.js');
addJavaScript('assets/js/uncompressed.jquery.dd.js');
addJavaScript('assets/js/uncompressed-jquery-ui-1.8.14.custom.js');
addJavaScript('assets/js/CPRuntimeDailog.js');
addJavaScript('assets/js/CPLibrary.js');
addJavaScript('assets/js/CPQuizLibrary.js');
addJavaScript('assets/js/CPPlaybar.js');
addJavaScript('assets/js/CPTOC.js');
addJavaScript('assets/js/CPWidgetManager.js');
addJavaScript('assets/playbar/playbarScript.js');
addJavaScript('Project.js');
document.getElementById('player').innerHTML = '\n <div class="cpMainContainer" id="cpDocument" style="left: 0px; top: 0px;">\n <div class="shadow" id="project_container" style="left: 0px; top: 0px; position: absolute;">\n <div id="project" class="cp-movie" style="width: 1280px; height: 720px">\n <div id="project_main" class="cp-timeline cp-main">\n <div id="div_Slide" onclick="handleClick(event)" style="top: 0px; width: 1280px; height: 720px; position: absolute; -webkit-tap-highlight-color: rgba(0,0,0,0);"><\/div>\n <\/div>\n <div id="autoplayDiv" style="display: block; text-align: center; position: absolute; left: 0px; top: 0px;">\n <img id="autoplayImage" src="" style="position: absolute; display: block; vertical-align: middle;" \/>\n <div id="playImage" tabindex="0" role="buton" aria-label="play" class="autoPlayButton" onkeydown="CPPlayButtonHandle(event)" onClick="cp.movie.play()" style="position: absolute; display: block; vertical-align: middle;"><\/div>\n <\/div>\n <\/div>\n <div id="playbar" style="left: 0px; float: left; position: absolute;"><\/div>\n <div id="toc" style="left: 0px; float: left; position: absolute;"><\/div>\n <div id="cc" style="left: 0px; float: left; position: absolute; visibility: hidden;">\n <div id="ccText" style="left: 0px; float: left; position: absolute; width: 100%; height: 100%;">\n <p style="margin-left: 8px; margin-right: 8px; margin-top: 2px;"><\/p>\n <\/div>\n <div id="ccClose" style="background-image: url(.\/assets\/Playbar_icons\/ccClose.png); right: 0px; float: right; position: absolute; cursor: pointer; width: 13px; height: 11px;" onclick="showHideCC()"><\/div>\n <\/div>\n <div id="passwordDiv" style="display: block; text-align: center; position: absolute; width: 100%; height: 100%; left: 0px; top: 0px;"><\/div>\n <div id="expDiv" style="display: block; text-align: center; position: absolute; width: 100%; height: 100%; left: 0px; top: 0px;"><\/div>\n <\/div>\n <\/div>\n <div id="blockUserInteraction" class="blocker" style="width: 100%; height: 100%;">\n <table style="width: 100%; height: 100%; text-align: center; vertical-align: middle;" id="loading" class="loadingBackground">\n <tr style="width: 100%; height: 100%; text-align: center; vertical-align: middle;">\n <td style="width: 100%; height: 100%; text-align: center; vertical-align: middle;">\n <image id="preloaderImage"><\/image>\n <div id="loadingString" class="loadingString">Loading...<\/div>\n <\/td>\n <\/tr>\n <\/table>\n <\/div>\n';
function DoCPInit()
{
CPPreInit();
cp.QuizLibraryInit();
CPPostInit();
}
document.oncomplete = DoCPInit();
}
}
I included all the functions for inserting files (internal and external CSS and JavaScript), in case the problem is there, though it took me several hours just to get the internal CSS function to work without disabling the JavaScript function. :S
When using the browser debugger in IE, it errors with the execution (as a part of the DoCPInit function) of the CPPreInit function, which is located in CPLibrary.js, the fifth script dynamically loaded. The debugger states that the function is undefined.
Honestly, I'd prefer to use PHP to get this all done, but my hands are tied, so JavaScript has to get the job done, and I know it can, I'm just screwing it up. :(
I have a suspicion that the complete event is not firing when you expect, primarily because you're loading in these scripts dynamically, so independently to the resources after which complete fires.
It seems to me you actually need to listen for the load event for each resource, count them and only when those are complete invoke DoCPInit
function resourceLoaded() {
var ev = new Event('resourceLoaded', {bubbles: true});
this.removeEventListener('load', resourceLoaded); // cleanup
this.dispatchEvent(ev);
}
function loadResource(parent, tag, args) {
var node = document.createElement(tag), arg;
// add listeners on this line if needed
node.addEventListener('load', resourceLoaded);
for (arg in args) node.setAttribute(arg, args[arg]);
parent.appendChild(node);
return node;
}
function addJavaScript(src) {
loadResource(
document.head,
{
'type': 'text/javascript',
'src': src
}
);
}
var waitfor = 1; // one script
document.addEventListener('resourceLoaded', function () {
if (--waitfor > 0) return;
// invoke whatever
DoCPInit();
});
Partially tested it in Chrome, you may need to check compatibility over browsers
Quick answer: use a script from http://microjs.com/#loader to load the scripts and execute something after a load. LABJs is a good choice.
Alright, so I took my cue from Paul S, but his code wasn't working. Adding alerts to the first line in the dynamically loaded scripts showed that the DoCPInit function was called before the scripts were loaded, which is where the error came from. To correct this, I moved the jQuery call to a standard SCRIPT tag and called the rest using $.AJAX() with async set to false. To simplify the matter, I set it using an array, rather than 10 individual calls.
var addJavaScript = [
'assets/js/uncompressed.jquery.dd.js',
'assets/js/uncompressed-jquery-ui-1.8.14.custom.js',
'assets/js/CPRuntimeDailog.js',
'assets/js/CPLibrary.js',
'assets/js/CPQuizLibrary.js',
'assets/js/CPPlaybar.js',
'assets/js/CPTOC.js',
'assets/js/CPWidgetManager.js',
'assets/playbar/playbarScript.js',
'Project.js',
];
jQuery.each(addJavaScript, function (i, val) {
jQuery.ajax({ async: false, dataType: 'script', url: val });
});

Trouble in synchronising and/or adding captions to images using caroufredsel plugin

I am new to this so please bear with me if my question is obviously stupid. I am trying to incorporate the slideshow as shown in this site using caroufredsel http://coolcarousels.frebsite.nl/c/48/
Now I needed to add captions to the images. so after trying to tweak the code unsuccessfully, I created a workaround where I created another carousel for just the captions and synchronized it with the main carousel. It worked that the captions appeared just fine but now I am not able to synchronize it with the click function/feature on the main carousel. if I comment the click function out , it works splendidly, but I need that click function. what am I doing wrong or is there an easier way to do what I want. I went thru the documentation and tried to incorporate a new div with id "item". but then the entire "pager" section disappears etc. I will include the full code here. thanks in advance for the help.
my css looks like this::
html, body {
height: 100%;
padding: 0;
margin: 0px;
}
body {
min-height: 400px;
}
#wrapper {
width: 697px;
height: 400px;
margin: -155px 0 0 -330px;
position: absolute;
top: 270px;
left: 50%;
box-shadow: 0px 1px 20px #c5a101;
border:3px solid #c5a101;
background-color: rgba(246,217,90,0.9);
}
#carousel {
width: 580px;
height: 360px;
overflow: hidden;
position: relative;
z-index: 2;
float:left;
}
#carousel img {
display: block;
float: left;
}
#pager {
width: 112px;
height: 350px;
overflow: hidden;
position: absolute;
top: 0;
right: 0;
}
#pager div {
height: 81px;
width: 100px;
box-shadow: 0px 0px 5px #c5a101;
}
#pager img {
cursor: pointer;
display: block;
height: 81px;
width: 112px;
margin-bottom: 5px;
float: left;
border:3px solid #c5a101;
cursor:default;
}
#texts-wrapper {
width: 700px;
height: 50px;
float: left;
}
#texts > div {
width: 300px;
height: 50px;
position: relative;
float: left;
margin-top: auto;
}
#texts > div > div {
width: 700px;
position: absolute;
left: 2px;
bottom: 5px;
float: right;
padding-top: 25px;
}
#texts a {
color:#083377;
font-family:Trajan Pro;
font-size: 16px;
font-weight: bold;
text-shadow: 0 1px 2px rgba(0,0,0,0.4);
text-decoration: none;
outline: none;
display: block;
background-color: rgba(248,229,145,0.4);
border: 1px solid rgba(246,217,90,0.4);
width: 700px;
height: 85px;
padding-left: 10px;
}
#texts a:hover {
background-color: rgba(255,208,0,0.9);
box-shadow: 0px 2px 15px #c5a101;
color: rgba(227,75,76,0.7);
}
my html code looks like this::
<div id="wrapper">
<div id="inner">
<div id="carousel">
<img src="img/building.jpg" width="580" height="350" />
<img src="img/guytalkingtokids.jpg" width="580" height="350" />
<img src="img/group.jpg" width="580" height="350" />
<img src="img/oath.jpg" width="580" height="350" />
<img src="img/finalists.jpg" width="580" height="350" />
</div>
<div id="pager"></div>
</div>
<div id="texts-wrapper">
<div id="texts">
<div>
<a style="text-decoration:none; " href="blank.html" >
<div><p>The red building across the street.</p> </div></a>
</div>
<div>
<a style="text-decoration:none;" href="blank.html" >
<div><p>How yall doin? blah blah</p> </div></a>
</div>
<div>
<a style="text-decoration:none;" href="blank.html" >
<div><p>Lotsa ppl!.</p></div></a>
</div>
<div>
<a style="text-decoration:none;" href="blank.html" >
<div><p>I put another caption!</p></div></a>
</div>
<div>
<a style="text-decoration:none;" href="blank.html" >
<div> <p>Yay! We won?! How?!</p></div></a>
</div>
</div>
</div>
</div>
And my script tag looks like::
$(function() {
var $carousel = $('#carousel'),
$pager = $('#pager');
// gather the thumbnails
var $thumb = $( '<div class="thumb" />' );
$carousel.children().each(function() {
var src = $(this).attr( 'src' );
$thumb.append( '<img src="' + src.split('/large/').join('/small/') + '" />' );
});
// duplicate the thumbnails
for (var a = 0; a < $carousel.children().length - 1; a++) {
$pager.append( $thumb.clone() );
}
// create large carousel
$carousel.carouFredSel({
items: {
visible: 1,
width: 580,
height: 350
},
//auto:false,/* temporary: to stop automatic scrolling */
scroll: {
fx: 'directscroll',
pauseOnHover:true,
duration: 500,
timeoutDuration: 5500,
onBefore: function( data ) {
var oldSrc = data.items.old.attr('src').split('/large/').join('/small/'),
newSrc = data.items.visible.attr('src').split('/large/').join('/small/'),
$t = $thumbs.find('img:first-child[src="' + newSrc + '"]').parent();
$t.trigger('slideTo', [$('img[src="' + oldSrc + '"]', $t), 'next']);
}
}
});
// create thumb carousels
var $thumbs = $('.thumb');
$thumbs.each(function( i ) {
$(this).carouFredSel({
auto: false,
scroll: {
fx: 'directscroll'
},
responsive:true,
items: {
start: i+1,
visible: 1,
width: 112,
height: 89.6
}
});
// click the carousel---> comment out this portion to disable the click function on small images
$(this).click(function() {
var src = $(this).children().first().attr('src').split('/small/').join('/large/');
$carousel.trigger('slideTo', [$('img[src="' + src + '"]', $carousel), 'next']);
});
});
// comment out the click function and uncomment this section of #texts to have a synchronised carousel with captions but no click function
$('#texts').carouFredSel({
items: 1,
direction: 'left',
responsive:true,
auto: {
play: true,
duration: 500,
easing: 'quadratic',
timeoutDuration: 5500
}
});
});
Now I used jquery version 1.8.2 and caroufredsel version 6.2.1. thanks again for the help in advance. sorry if my code looks messy as well.Latest update as of 3/22/2014:: I went thru the documentation of the plugin CarouFredSel and stumbled upon one of the settable parameters/ configurations called "synchronise". If I understood it right, I can synchronise 2 carousels to respond to the same event. So i added the line of code "synchronise:{"#carousel"} into the text carousel to tell it to synchronise it with the main carousel...
$('#texts').carouFredSel({
items: 1,
direction: 'left',
responsive:true,
synchronise:{"#carousel"},/*This is the new line I added*/
auto: {
play: true,
duration: 500,
easing: 'quadratic',
timeoutDuration: 5500
}
});
Unfortunately that did not work as well. Now there is no timing pattern as well. everytime I click the small image it just went ahead in position at a random rate. So I am still stuck with the same problem if not making it worse. Thanks for the help in advance.
After fighting with the problem for more than a week, I was able to figure out a solution to my problem. Now it may not be the best solution but it worked and hence I am posting it so that in future if somebody else has the same or similar problem, it may be of help.Now if anyone came up with a solution that works better, I would still like you to post it here for I may want to learn what you did, why you did it and learn from the experience. I dont claim to be an expert programmer. I am still learning and this site has been a great learning tool for me so far.Anyway coming back to the problem, I added this piece of code...
//sais try: synchronise texts and carousel to work together and on click
var index = $(this).triggerHandler( 'currentPosition' );
if ( index == 0 ) {
index = $(this).children().length;
}
// trigger the titles carousel
$('#texts').trigger('slideTo', [ index, 'next' ]);
right here...
// create large carousel
$carousel.carouFredSel({
items: {
visible: 1,
width: 580,
height: 350
},
//auto:false,/* temporary: to stop automatic scrolling */
scroll: {
fx: 'directscroll',
pauseOnHover:true,
duration: 500,
timeoutDuration: 3500,
onBefore: function( data ) {
var oldSrc = data.items.old.attr('src').split('/large/').join('/small/'),
newSrc = data.items.visible.attr('src').split('/large/').join('/small/'),
$t = $thumbs.find('img:first-child[src="' + newSrc + '"]').parent();
$t.trigger('slideTo', [$('img[src="' + oldSrc + '"]', $t), 'next']);
/* [ the code goes here!]*/
now with that i was able to synchronise the carousels (#carousel, #texts) together to work with the click function/feature as well. Also I had tried to synchronise the carousel using a synchronise function thats in carouFredSel. Well take that out. It did not work.I dont know if this is going to be useful to anyone else but if it did, thats great. But again if somebody came up with a better solution please do let me know as well. Thanks. Keep up the goo work

Categories