如何在点击时弹出图像?

时间:2015-01-02 05:21:03

标签: javascript jquery css

所以我的增量已经走了很长一段路,我在这里得到了帮助来制作原始弹出窗口并且效果很好,现在我真的想让弹出窗口成像?

到目前为止,我有:

$("#coffeeButton").click(function(e) {
    var obj = $("#clone").clone();
    $("body").append(obj);
    obj.html("+"+ cookRate);
    coffee += cookRate;
    totalCoffee += cookRate;
    document.getElementById("coffee").innerHTML = prettify(coffee);
    document.getElementById("totalCoffee").innerHTML = prettify(totalCoffee);

    obj.css('position','absolute');
    obj.offset({left: e.pageX-10, top: e.pageY-25});

    obj.animate({"top": "-=80px"}, 1000, "linear", function() {
        $(this).remove();
    });     
});

并且效果很好并且给了我一个弹出窗口,但是我该怎么做才能成为一个图像呢?

我尝试过创建一个div,将一个图像放在div中,而不是调用div上的“clone”...

我是在正确的轨道上吗?

我想从Cookie点击器中获取的示例 - http://orteil.dashnet.org/cookieclicker/

到目前为止我的游戏 - retiredgamers.net /

jsfiddle - http://jsfiddle.net/edznycyy/6/

2 个答案:

答案 0 :(得分:0)

你可能有很多方法可以做到这一点。为了使事情尽可能接近你现在所能做的,我会做什么...基本上只创建2个元素,第一个是你的数字,第二个是图像,确保数字在图像上方css' s z-index,然后将它们串联起来动画:

这里是working jsfiddle

HTML更改:

<img title = "" id="myImg" src="http://blog.beautyfix.com/wp-content/uploads/2012/09/Coffee_iStock_000006160362Medium.jpg" style="display:none" width="30" height="30"> 

Javascript更改:

$("#coffeeButton").click(function(e) {

    var obj = $("#clone").clone(); //im guessing you chose to use clone here to make it easier to work with the object, so I did the same for the image
    var img = $("#myImg").clone(); 

    $("body").append(obj);
    $("body").append(img);

    obj.html("+"+ cookRate);
    coffee += cookRate;
    totalCoffee += cookRate;
    document.getElementById("coffee").innerHTML = prettify(coffee);
    document.getElementById("totalCoffee").innerHTML = prettify(totalCoffee);

    obj.css('position','absolute'); 
    obj.css('z-index', '2');
    img.css('position','absolute');
    img.show();

    obj.offset({left: e.pageX-10, top: e.pageY-25});
    img.offset({left: e.pageX-10, top: e.pageY-25});

    obj.animate({"top": "-=80px"}, 1000, "linear", function() {
        $(this).remove();
    }); 
    img.animate({"top": "-=80px"}, 1000, "linear", function() {
        $(this).remove();
    });

});

答案 1 :(得分:0)

如果您不想自己动手,可以使用jquery插件轻松完成。我确信还有更多内容,

这是我找到的!

Here is the demo page for full-screan popup plugin

....
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="jquery.fullscreen-popup.js"></script>
....

<a class="open-popup" href="#popup">Open popup</a>

...

<div id="popup" style="display: none; width: 640px">
  ...
</div>

<script>

    $(function() {
      $(".open-popup").fullScreenPopup({
        // Options
      });
    });

</script>

Example is from hereyou can download the plugin from here以及a list that contains many more