Content Scrollbar (Revised)

This is a simple fix, change the following function so that it is available publicly.

Doing this is simple, the keyword private on the first line needs to be turned into public


   private function setScrollProps():Void
   {
      // if either viewable or content hasn't been set we cannot successfully set our scroll properties
      // so return out of the function
      if (viewable == undefined || content == undefined) return;

      // if not then set properties
      scrollable = content._height - viewable.height;
      top_scroll = content._y;

      initElements();
      checkScrollability();
   }

Your function will now look like this:


   public function setScrollProps():Void
   {
      // if either viewable or content hasn't been set we cannot successfully set our scroll properties
      // so return out of the function
      if (viewable == undefined || content == undefined) return;

      // if not then set properties
      scrollable = content._height - viewable.height;
      top_scroll = content._y;

      initElements();
      checkScrollability();
   }

Now, in the onLoad function you are assigning your XML object, when all the text is updated call the setScrollProps() method on the scrollbar.

And Viola!

Take Care.
_Michael

I feel so stupid, not enough as knowledge (jet) but it’s hard to learn :slight_smile:

i added the files i’m using, if you can take the time to look at it. What is going wrong?
or what am i doing wrong :crying:

Where is the XML being loaded in your file? Can you paste that line of code, and maybe tell us where it is, I am unable to open and search your file at the moment. Thanks.

TakeCare.
_Michael

This is the as script for loading the xml
it’s in the in the movieclip within the contentMain called Movieclip called objetas


/*************************/
var nieuweXML:XML = new XML ();
nieuweXML.ignoreWhite = true;
nieuweXML.load ('news.xml');
bt._visible = false;
nieuweXML.onLoad = function ()
{
    var childs:XMLNode = nieuweXML.firstChild;
    var childTotal:Number = childs.childNodes.length;
    _global.fcn = function ()
    {
        for (var i = 0; i < childTotal; i++)
        {
            if (i != selecteer)
            {
                _parent.nieuws_mc['bt' + i].enabled = true;
            }
        }
    };
    for (var i = 0; i < childTotal; i++)
    {
        bt.duplicateMovieClip ('bt' + i, _parent.nieuws_mc.getNextHighestDepth ());
        /*************************/
        _parent.nieuws_mc.bt0.enabled = true;
        _parent.nieuws_mc.bt0.useHandCursor = true;
        /*************************/
        var datum_xml = (childs.childNodes*.childNodes[0].firstChild.nodeValue);
        var titel_xml = (childs.childNodes*.childNodes[1].firstChild.nodeValue);
        var bericht_xml = (childs.childNodes*.childNodes[2].firstChild.nodeValue);
        /*************************/
        _parent.nieuws_mc['bt' + i].datum_txt.html = true;
        _parent.nieuws_mc['bt' + i].datum_txt.htmlText = datum_xml + "<br /><font color='#ffffff'>" + bericht_xml + "</font><br /><br />";
        _parent.nieuws_mc['bt' + i].datum_txt.autoSize = true;
        _parent.nieuws_mc['bt' + i].titel_txt.html = true;
        _parent.nieuws_mc['bt' + i].titel_txt.htmlText = titel_xml;
        /*************************/
        _parent.nieuws_mc['bt' + i].i = i;
        var eersteDatum_xml = childs.childNodes[0].childNodes[0].firstChild.nodeValue;
        var eersteTitel_xml = childs.childNodes[0].childNodes[1].firstChild.nodeValue;
        var eersteBericht_xml = childs.childNodes[0].childNodes[2].firstChild.nodeValue;
        /*************************/
        _parent.nieuws_mc['bt' + i].datum_xml = datum_xml;
        _parent.nieuws_mc['bt' + i].titel_xml = titel_xml;
        _parent.nieuws_mc['bt' + i].bericht_xml = bericht_xml;
        /*************************/
        _parent.nieuws_mc['bt' + i].fd_txt._height = _parent.nieuws_mc['bt' + i].datum_txt._height + 4;
        _parent.nieuws_mc['bt' + i]._y = _parent.nieuws_mc['bt' + (i - 1)]._y + _parent.nieuws_mc['bt' + (i - 1)]._height + 2;
        /*************************/
    }
};

Thanks

Okay, here is the first code change…

Where you are defining the onLoad event, notice the addition just before the last brace }


...

        _parent.nieuws_mc['bt' + i].fd_txt._height = _parent.nieuws_mc['bt' + i].datum_txt._height + 4;
        _parent.nieuws_mc['bt' + i]._y = _parent.nieuws_mc['bt' + (i - 1)]._y + _parent.nieuws_mc['bt' + (i - 1)]._height + 2;
        /*************************/
    }
    _root.myscrollbar.setScrollProps();
    
};

Now you’ll also need to change the MyScrollbar class.

In the checkScrollability definition, you need to add an else statement that will turn the dragger back visible if you are able to scroll.

The new checkScrollability method.


    public function checkScrollability ():Void
    {
        if (scrollable < 0)
        {
            dragger._visible = false;
            track._alpha = 0;
        }
      else
      {
         dragger._visible = true;
      }
    }

And This works with your code, for it is the one I tested with.

Take Care.

_Michael
[FONT=Courier New][/FONT]

Ok, I honestly can’t figure out why this is not working for me … can anyone help me out? Here is the .fla the .as and the .swf …

I tried to follow all of Michael’s directions to the point but failed aparently …

Thanks!

Hi everyone,

First off, thanks for all the great tutorials… it is greatly appeciated. I have recommended this site to many many people!

My question is in regards to displaying dynamic information without having to use dynamic text. Basically i want to load in different mc’s depending on what button is pressed. I created a container that loads in a different mc depending on the button pressed, however i can’t get the scroller to reset to the top as well as call the function for the visibility for the scroller. Some mc’s contain information that needs to be scrolled and some do not. I do apologize for the noob question, i am trying to teach myself, but heavens knows i need to sit down and learn actionscript properly. I have changed the functions from private to public and called the setScrollProps(); function from the button on release. When i click on the button though the mc starts at the last located that the scrollbar was on the last mc.

Ok now i am confusing myself. Here are the files: Thanks for all your time and hard work thus far!

http://www.e-mergedesign.com/DynamicScroller.rar

Martha Stewart.

Okay just so you can get this working, I have a very ugly workaround, for the time being.

First here is the workaround…

on your buttons set a timeout that will call the myscrollbar setScrollProps method after, say, 50 milliseconds.

Just add the last line onto each button.


on (release) {
	contentMain.unloadMovie
	contentMain.attachMovie("abouttxt","newcontent",1);
	
        // Line of importance
        setTimeout(_root.myscrollbar, "setScrollProps", 50);
}

Then in the MyScrollbar class, in the checkScrollability method, you need to have an option that will turn the scrollbar back on… Here is what your new checkScrollability will look like…



   // checks whether or not our content is scrollable, if it is not than we hide the dragger and disable the
   // other elements
   public function checkScrollability():Void
   {
      if (scrollable < 0)
      {
         dragger._visible = false;
         up._alpha = 50;
         down._alpha = 50;
         track._alpha = 50;
         return;
      }
      dragger._visible = true;
      up._alpha = 100;
      down._alpha = 100;
      track._alpha = 100;
   }

Okay now like I mentioned earlier, this is just an ugly work around.

What you really need to do is load those SWF’s using the MovieClipLoader class, and in the onLoadInit() even you’ll need to call the setScrollProps on myscrollbar.

I hope this made some sense.

Take Care.
_Michael

Hi MichaelxxOA,

Now that is what i call a quick reply…thanks so much for your time.

i will try implement your suggestions and let you know how it goes.

Thanks again,

Martha Stewart

@BoonDock

You actually mixed together, two example files. If you are going to use a Rectangle as your viewable area, you’ll need to use the class I posted a few posts ago… I will post again here though to avoid confusion.

Once I plugged this Class in, it all worked fine.


import flash.geom.Rectangle;
class MyScrollbar
{
	public var content:MovieClip;
	public var viewable:Rectangle;
	public var dragger:MovieClip;
	public var track:MovieClip;
	public var left:Number;
	public var top:Number;
	public var right:Number;
	public var bottom:Number;
	public var move_speed:Number = 3;
	public var scrollable:Number = 0;
	public var top_scroll:Number = 0;
	public function MyScrollbar ()
	{
		left = track._x;
		top = track._y;
		right = track._x;
		bottom = track._y + (track._height - dragger._height);
		Mouse.addListener (this);
	}
	public function setContent (content:MovieClip):Void
	{
		this.content = content;
		setScrollProps ();
	}
	public function setViewable (viewable:Rectangle):Void
	{
		this.viewable = viewable;
		setScrollProps ();
	}
	public function setEasing (value:Boolean, speed:Number):Void
	{
		content.isEasing = value;
		if (speed > 0 && value)
		{
			content.speed = speed;
		}
	}
	public function updateContentPos ():Void
	{
		var percent_scrolled:Number = (dragger._y - track._y) / (track._height - dragger._height);
		content.newY = Math.round (top_scroll - (percent_scrolled * scrollable));
	}
	public function setScrollProps ():Void
	{ 
      trace("Hello");
		if (viewable == undefined || content == undefined)
		{
			return;
		}
		scrollable = content._height - viewable.height;
		top_scroll = content._y;
		initElements ();
		checkScrollability ();
	}
	public function checkScrollability ():Void
	{
		if (scrollable < 0)
		{
			dragger._visible = false;
			track._alpha = 0;
		}
      else
      {
         dragger._visible = true;
      }
	}
	public function onMouseWheel (delta:Number):Void
	{
		var speed:Number = (delta > 0) ? move_speed : -move_speed;
		dragger._y = Math.min (bottom, Math.max (top, dragger._y - speed));
		updateContentPos ();
	}
	public function initElements ():Void
	{
		var src;
		dragger.src = this;
		track.src = this;
		var newY;
		var isEasing;
		var speed;
		content.speed = move_speed;
		content.onEnterFrame = function ()
		{
			if (this.isEasing)
			{
				this._y += (this.newY - this._y) >> this.speed;
				return;
			}
			this._y = this.newY;
		};
		dragger.onPress = function ()
		{
			this.startDrag (false, this.src.left, this.src.top, this.src.right, this.src.bottom);
			this.onMouseMove = function ()
			{
				this.src.updateContentPos ();
			};
		};
		dragger.onMouseUp = function ()
		{
			stopDrag ();
			delete this.onMouseMove;
		};
	}
}

Take Care.
_Michael

@Martha Stewart

No worries, just glad that I could help.

Take Care.
_Michael

Wow thanks Michael! That worked just fine! Kudos! :thumb:

Ok I’m back,

Well i feel like i just made huge progress, even if it’s a ugly workaround as you say. The funny part is i just wouldn’t know a better way so it looks perfect to me.

But… heh heh… now i am looking for a way to reset the scroller back to the top again. Another words now if i scroll down when viewing the “abouttxt” mc and then press the button to load the “historytxt” mc the history mc will load at the location that the scroller was previously at. I hope you make sense of my ramblings… thanks again.

On a side note i noticed a small bug on the forum… if i press the “post reply” button on the bottom of the forum and then press the back button on my browser, the browser crashes. doesn’t bother me… small price to pay for all the information and help i get from here but thought i would let you know.

Firefox 1.0.7
xp sp2

Thanks AGAIN,

Martha Stewart


I have no sig.

This is actually something I can’t believe I overlooked in the initial design…

We need a simple backToTop method, add this to the end of your class, just before the last closing brace }.


   /**
    * Moves the dragger back to the top of the scroll, and resets the position of the content.
    */
   public function backToTop() : Void
   {
      dragger._y = top;
      updateContentPos();
   }

Now anytime you’d like to go back to the top of the scroll, you simple execute the backToTop on myscrollbar, or any instances of the MyScrollbar class.

Take Care.
_Michael

Hi MichaelxxOA,

sorry to bother you again… i tried as much as i could to do this without any more help. Could you have a look at my code and tell me why when i scroll down on the initial mc which is “about”, and then click on the “history” button and then back on the “about” button that the content does not move back to the top?

I changed the delay of 50 to 0 for the setScrollProps which seemed to help it from slowly shifting down everytime but i changed it back in my code so you can see what is happening.

I will make sure i spread the gospel of kirupa to everyone i know!

And i beg of you not to mock my “ugly” call method of the backtotop function.

http://www.e-mergedesign.com/ScrollerTest2.rar

Begging for Acceptance,

Martha Stewart


I have no sig.

Ok I think i got it!

If i was a woman and you were a man… ok maybe this isn’t the place to do this…

anyways:

I added content._y = top; into the backToTop() function.

/**
* Moves the dragger back to the top of the scroll, and resets the position of the content.
*/
public function backToTop():Void
{
dragger._y = top;
content._y = top;
updateContentPos();
}

and my onrelease button looks like this:

on (release) {
contentMain.unloadMovie;
contentMain.attachMovie(“abouttxt”,“newcontent”,1);

    // Line of importance
    setTimeout(_root.myscrollbar, "setScrollProps", 50);
	myscrollbar.backToTop();

}

Thanks for all your input and help. I will post my final project here when it is done so at least you know why you were helping.

Have a great day,

Martha Stewart


I have no sig.

:slight_smile: Very good job, I hope that I was of some help, if you need anything else feel free to ask.

Take Care.
_Michael

I am trying not to flood this forum…:devious:

Hopefully my last question,

Now what i am trying to accomplish is taking the scrollbar and contentMain and placing it inside a movieclip.

The reason is so i can reuse the same movieclip now called “Scroller” and place it inside of all five pages i will have for the main site. I can also create a simply fade for each of the different sub page transitions by manipulating the entire “scroller” mc.

Here is my code once again to hopefully illustrate better as to what i mean.

http://www.e-mergedesign.com/ScrollerTest2.rar

I hope you get paid something for you time here,

Forgot to say what the problem was…everything works great except that when i click on history the scroller still scrolls to the size of the initial mc that was loaded…“about”. Everythings else is just great though.

Martha Stewart


I have no sig.

Ok, sorry Michael … I don’t know what you did that I can’t do, but is it possible that you could step me through what I need to do with everything … because it’s still isn’t working for me …

If you need the .fla could u just grab it from the previous posts …

Thanks a ton! :wink: :thumb:
Boon.

I really feel one of the stupid guys…
Michael helps a lot but i just don’t get any result. And i can’t figure out why?

please check http://www.dri360.com/flash_main.php and then press “nieuws”

The scroller just doesn’t move.

The files from this thing are at http://www.dri360.com/news.zip

Is there someone who can help me, above all of that wants to take the time to help me?

Thanx. Jos