/    Sign up×
Community /Pin to ProfileBookmark

Help with this code to work with only 1 picture

Hi everyone,

I have this piece of code a friend had done for me but I can’t seem to reach him and I need a change. I am learning js – I get fascinated but yet ineffective. LOL

The js code below was used for a background. It picks images from a list, rotates them using a fade in/fade out action. And also, there is a resize function applied to those pictures, so they always fit nicely.

I need to use this code in a part of the website where there should be only one picture. So I need to keep the resize function and the preloading part. How would you change that code?

And please, feel free to comment about this one and/or explain your solution; it helps me learn. THANK YOU GUYS!

[code=html]
<script type=”text/javascript”>
//<![CDATA[

// background image ratio (width/height)
// use to resize background to get best view
var backgroundRatio = 1.9;

function preload_bg_images() {
new Ajax.Updater(‘slide-images’, ‘get_images.php?what=bg’, {
asynchronous: true,
onComplete: init_slideshow
});
}

function init_slideshow() {
resize_background();

var lis = $(‘slide-images’).getElementsByTagName(‘li’);
for( i=0; i < lis.length; i++){
lis[i].style.display = ‘none’;
}
end_frame = lis.length -1;

if (lis.length == 1) {
// just appear the image
Effect.Appear(lis[0], { duration: 1.0, from: 0, to: 1 });
} else {
// start rotate background images
start_slideshow(0, end_frame, 0);
}
}

function start_slideshow(start_frame, end_frame, delay) {
setTimeout(fadeInOut(start_frame, start_frame, end_frame, 3000), 0);
}

function fadeInOut(frame, start_frame, end_frame, delay) {
return (function() {
lis = $(‘slide-images’).getElementsByTagName(‘li’);
Effect.Fade(lis[frame]);
if (frame == end_frame) { frame = start_frame; } else { frame++; }
lisAppear = lis[frame];
setTimeout(“Effect.Appear(lisAppear);”, 0);
setTimeout(fadeInOut(frame, start_frame, end_frame, delay), delay + 3000);
})
}

function resize_background() {
var width = $(‘slide-images’).getWidth();
var height = $(‘slide-images’).getHeight();
var ratio = (width * 1.0) / height;

var top = 0;
var left = 0;
if (ratio > backgroundRatio) {
height = width / backgroundRatio;
top = -(ratio – backgroundRatio) * height / 2;
} else {
width = height * backgroundRatio;
left = -(backgroundRatio – ratio) * width / 2;
}

var lis = $(‘slide-images’).getElementsByTagName(‘li’);
for( i=0; i < lis.length; i++){
// resize image
lis[i].style.top = 70 + top + “px”;
lis[i].style.left = left + “px”;
lis[i].style.width = width + “px”;
lis[i].style.height = height + “px”;
}
}

// fetch images after page is loaded, then start background image rotation
Event.observe(window, ‘load’, preload_bg_images);
Event.observe(window, ‘resize’, resize_background);
//]]>
</script>
[/code]

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @joomoo79 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...