LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-02-2010, 11:09 AM   #1
bloodyscript
Member
 
Registered: Apr 2006
Distribution: Sabayon linux 5.1
Posts: 182

Rep: Reputation: 15
javascript image sliding direction?


ok im trying to learn javascript. so i choose to start out making a image slideshow banner that auto scrolls on load no special buttons. so since i had no idea were to start i found code that was free to use commercial and non commercial and ill be able to change it. heres that code:

Code:
var SLIDETIMER = 3;

var SLIDESPEED = 3;

var SCROLLTIMER = 3;

var SCROLLSPEED = 3;

var STARTINGOPACITY = 40;





// handles section to section scrolling of the content //

function slideContent(id,prefix,timer) {

  var div = document.getElementById(id);

  var slider = div.parentNode;

  clearInterval(slider.timer);

  slider.section = parseInt(id.replace(/\D/g,''));

  slider.target = div.offsetTop;

  slider.style.top = slider.style.top || '0px';

  slider.current = slider.style.top.replace('px','');

  slider.direction = (Math.abs(slider.current) > slider.target) ? 1 : -1;

  slider.style.opacity = STARTINGOPACITY * .01;

  slider.style.filter = 'alpha(opacity=' + STARTINGOPACITY + ')';

  slider.timer = setInterval( function() { slideAnimate(slider,prefix,timer) }, SLIDETIMER);

}



function slideAnimate(slider,prefix,timer) {

  var curr = Math.abs(slider.current);

  var tar = Math.abs(slider.target);

  var dir = slider.direction;

  if((tar - curr <= SLIDESPEED && dir == -1) || (curr - tar <= SLIDESPEED && dir == 1)) {

    slider.style.top = (slider.target * -1) + 'px';

	slider.style.opacity = 1;

	slider.style.filter = 'alpha(opacity=100)';

    clearInterval(slider.timer);

	if(slider.autoscroll) {

	  setTimeout( function() { autoScroll(slider.id,prefix,timer) }, timer * 1000);

	}

  } else {

	var pos = (dir == 1) ? parseInt(slider.current) + SLIDESPEED : slider.current - SLIDESPEED;

    slider.current = pos;

    slider.style.top = pos + 'px';

  }

}



// handles manual scrolling of the content //

function scrollContent(id,dir) {

  var div = document.getElementById(id);

  clearInterval(div.timer);

  var sections = div.getElementsByTagName('div');

  var length = sections.length;

  var limit;

  if(dir == -1) {

    limit = 0;

  } else {

    if(length > 1) {

      limit = sections[length-1].offsetTop;

    } else {

      limit = sections[length-1].offsetHeight - div.parentNode.offsetHeight + 20;

    }

  }

  div.style.opacity = STARTINGOPACITY * .01;

  div.style.filter = 'alpha(opacity=' + STARTINGOPACITY + ')';

  div.timer = setInterval( function() { scrollAnimate(div,dir,limit) }, SCROLLTIMER);

}



function scrollAnimate(div,dir,limit) {

  div.style.top = div.style.top || '0px';

  var top = div.style.top.replace('px','');

  if(dir == 1) {

	if(limit - Math.abs(top) <= SCROLLSPEED) {

	  cancelScroll(div.id);

	  div.style.top = '-' + limit + 'px';

	} else {

	  div.style.top = top - SCROLLSPEED + 'px';

	}

  } else {

	if(Math.abs(top) - limit <= SCROLLSPEED) {

	  cancelScroll(div.id);

	  div.style.top = limit + 'px';

	} else {

	  div.style.top = parseInt(top) + SCROLLSPEED + 'px';

	}

  }

}



// cancel the scrolling on mouseout //

function cancelScroll(id) {

  var div = document.getElementById(id);

  div.style.opacity = 1;

  div.style.filter = 'alpha(opacity=100)';

  clearTimeout(div.timer);

}



// initiate auto scrolling //

function autoScroll(id,prefix,timer,restart) {

  var div = document.getElementById(id);

  div.autoscroll = (!div.autoscroll && !restart) ? false : true;

  if(div.autoscroll) {

    var sections = div.getElementsByTagName('div');

    var length = sections.length;

    div.section = (div.section && div.section < length) ? div.section + 1 : 1;

    slideContent(prefix + '-' + div.section,prefix,timer);

  }

}



// cancel automatic scrolling //

function cancelAutoScroll(id) {

  var div = document.getElementById(id);

  div.autoscroll = false;

}
now my question is how would i manage to change the direction of the slide? the code itself is supposed to do multiple functions; for a image slide show, news ticker, and text scroll. so to me its a little confusing at the moment its sliding vertically i would like it to slide horizontally.
 
Old 06-05-2010, 07:52 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
You should look into the function slideContent. Both the direction are set (slider.direction) and the postion of the object (slider.style.top). Change the direction by giving slider.direction a different value, and I think you can set the horizontal position by using slider.style.left instead op slider.style.top, but you should check the properties of the parentNode object.

Furthermore, this is one of the best web sites I know for studying web programming: http://www.w3schools.com/

jlinkels
 
1 members found this post helpful.
Old 06-06-2010, 01:32 PM   #3
bloodyscript
Member
 
Registered: Apr 2006
Distribution: Sabayon linux 5.1
Posts: 182

Original Poster
Rep: Reputation: 15
thank you for the help and advice
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sliding Notifications in KDE? Jmanfoo Linux - Desktop 1 07-10-2008 12:55 AM
Javascript operating on more than one image when only told to do one... RHLinuxGUY Programming 1 12-08-2006 11:36 AM
Javascript dragging image 0raven0 Programming 2 01-13-2006 11:59 PM
JavaScript PreLoad Image andrewt Programming 1 08-25-2003 03:12 AM
sliding the screen over (xvidtune) philfighter Linux - Software 2 08-21-2001 03:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration