I have an image and a button within a panel.
When the button is 开发者_开发知识库clicked I would like my image to be replaced with another image at random based on an array of stored images.
I'm stuck on implementing a change image function within the button.
Assistance would be appreciated.
You could do it using jQuery by:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
// Handler for .ready() called.
var imageList = ['one.png', 'two.png', 'three.png', 'four.png'];
$('#btn').click(function(){
var imgName = imageList[Math.floor(Math.random()*imageList.length)];
$('#image').attr('src', imgName);
});
});
</script>
<button id="btn">Click Me</button>
<img id="image" src="someimage.png" />
refToYourImage.onclick = function() {
refToYourImage.src = arrOfRandomImages[Math.floor(Math.random() * arrOfRandomImages.length)];
}
Something like this?
<img id = "IM" src = "someImageReference"/>
<button onclick = "changeImage(document.getElementById('IM'))">Change image</button>
<script type = "text/javascript">
function changeImage(image) {
var images = new Array{"image paths","here"};
var rand = Math.round(Math.random() * images.length);
image.src = images[rand];
}
</script>
The best would be to have a separate panel with a card layout for the images. This way you can easily add animations when swiching the images.
Example
new Ext.Panel({
id:'imagePanel',
layout: 'card',
cardSwitchAnimation: 'fade',
items:[
{html:'<img src="/img1.jpg" />'},
{html:'<img src="/img2.jpg" />'},
{html:'<img src="/img3.jpg" />'},
{html:'<img src="/img4.jpg" />'}
]
});
And then have this to switch the image
Ext.getCmp('imagePanel').setActiveItem(Math.floor(Math.random()*numImages);
精彩评论