Hi,
I need some images on my website that change when the user hovers his mouse over them (and change back when the mouse leaves again).
I know the concept behind this, one simply uses the 'onmouseover' and 'onmouseout' client-side events and set the 'src' property of the image there. Usually one would also preload the images, otherwise it takes a while before the image is first loaded when the mouse hovers over the image. That happens like this
well, something like that at least, this is from memory.Code:var otherImage = new Image(); otherImage .src = 'myimage.jpg'; function ChangeImage(img) { img.src = otherImage.src; }
Anyway, this is all easy enough, but my problem is that the images I am talking about will be added during run-time, by the user of the site. The idea is that he can add images dynamically (by selecting two images with a FileUpload control and uploading them).
So to make this process easier, I decided to create my own image control that inherits Image:
The idea is that I can add these dynamically:vb.net Code:
Namespace Controls Public Class ImageControl Inherits Image Public Sub New(ByVal img1 As String, ByVal img2 As String) Me.Image1 = img1 Me.Image2 = img2 Me.ImageUrl = img1 If Not String.IsNullOrEmpty(Me.ImageR) Then Me.Attributes.Add("onmouseover", String.Format("this.src='{0}'", Me.ImageR)) Me.Attributes.Add("onmouseout", String.Format("this.src='{0}'", Me.ImageL)) End If End Sub Private _Image1 As String Public Property Image1() As String Get Return _Image1 End Get Set(ByVal value As String) _Image1 = value End Set End Property Private _Image2 As String Public Property Image2() As String Get Return _Image2 End Get Set(ByVal value As String) _Image2 = value End Set End Property End Class End Namespace
and that's it, no script needed, that's everything I need.vb.net Code:
Dim img As New ImageControl("myimage1.jpg", "myimage2.jpg") placeHolder.Controls.Add(img)
At the moment this works fine, on mouse-hover the image changes. However, there is no pre-loading, so sometimes it takes a while before the image changes.
So I now want to pre-load the images. But I don't know how I would do this easily... I doubt I can add the required script to the ImageControl somehow (or can I?), but I don't know where else to do it.
So what I need basically is a way to have my website add the required pre-loading javascript automatically. At the moment I am simply looping through my datasource (which supplies the image paths) and adding an ImageControl for every set of two images. So if I have, for example, this data:
then I want my website to automatically come up with this script, or something similar;Code:ID Img1 Img2 ---------------------------- 1 xyz.jpg xyz2.jpg 2 abc.jpg abc2.jpg 3 last.jpg last2.jpg
How can I do this?Code:<head> <script type="text/javascript"> var img1_1 = new Image(); var img1_2 = new Image(); var img2_1 = new Image(); var img2_2 = new Image(); var img3_1 = new Image(); var img3_2 = new Image(); img1_1.src = 'xyz.jpg'; img1_2.src = 'xyz2.jpg'; img2_1.src = 'abc.jpg'; img2_2.src = 'abc.jpg'; img3_1.src = 'last.jpg'; img3_2.src = 'last.jpg'; </script> </head>




Reply With Quote