[单击按钮时更改图像

问题描述 投票:0回答:4

我在面板上有一个图像和一个按钮。

单击按钮时,我希望根据存储的图像数组将我的图像随机替换为另一图像。

我坚持在按钮内实现更改图像功能。

将不胜感激。

javascript extjs sencha-touch dom-events
4个回答
1
投票

您可以通过以下方式使用jQuery:

<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" />

0
投票
refToYourImage.onclick = function() {
    refToYourImage.src = arrOfRandomImages[Math.floor(Math.random() * arrOfRandomImages.length)];
}

0
投票

这样的事情?

<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>

0
投票

最好的方法是使用一个单独的面板,并在图像上使用卡片布局。这样,您可以在对图像进行剪切时轻松添加动画。

示例

   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" />'}
]
    });

然后用它来切换图像

Ext.getCmp('imagePanel').setActiveItem(Math.floor(Math.random()*numImages);
© www.soinside.com 2019 - 2024. All rights reserved.