简体   繁体   English

jQuery动画,隐藏到特定位置

[英]jQuery animate, hide to a certain position

I'm kinda new to jQuery and have a question about the hide() function. 我是jQuery的新手,并且对hide()函数有疑问。 Is it possible to hide a box to a certain position? 是否可以将盒子隐藏到某个位置?

I created a quick jsfiddle, which I hope it demonstrates what I mean. 我创建了一个快速的jsfiddle,希望它能演示我的意思。 I placed a green box in a random position, behind the big red box, and want to make it look like the big red box scales down and vanishes in the green box. 我将一个绿色框随机放置在大红色框的后面,希望使其看起来像大红色框缩小并消失在绿色框中。

I tried googling it, but I'm a little lost on words for it, so I hope someone in here could give me a hint. 我尝试使用Google进行搜索,但是对此却有些措手不及,所以我希望这里的人能给我一些提示。

This is what I tried so far 这是我到目前为止尝试过的

// jQuery
$('.close_this').click(function () {
    $('#hide_box').hide(1000);
});

// CSS
#hide_to_this {
    background:green;
    width:100px;
    height:100px;
    position:absolute;
    top:300px;
    left:500px;
}
#hide_box {
    background:red;
    width:1000px;
    height:1000px;
    position:absolute;
    top:0;
}

// HTML
<div id='hide_to_this'></div>
<div id='hide_box'><a href='#' class='close_this'>Close</a></div>

jsFiddle jsFiddle

I hope this is what your're looking for. 希望这是您要寻找的。 As you can see, in order to hide an element to a certain position you could use animate() method 如您所见,为了将元素隐藏到某个位置,您可以使用animate()方法

// HTML
<div id='hide_to_this'></div>
<div id='hide_box'><a href='#' class='close_this'>Close</a></div>

// CSS
#hide_to_this {
    background:green;
    width:100px;
    height:100px;
    position:absolute;
    top:250px;
    left:400px;
}
#hide_box {
    background:red;
    width:250px;
    height:250px;
    position:absolute;
    top:0;
}

// jQuery
$(".close_this").click(function() {
    o = $('#hide_to_this').offset();
    w =  $('#hide_to_this').width();
    h =  $('#hide_to_this').height();  
    $("#hide_box").animate({
        opacity: 0.1,
        width: 0,
        height: 0,  
        left: o.left + w / 2,
        top: o.top + h / 2 
    }, 1000, function() {
        console.log('Done!');
    });
});

You may change the width and height of #hidebox to be equal to #hide_to_this element; 您可以将#hideboxwidthheight #hidebox为等于#hide_to_this元素; you can change the speed to something faster or slower than 1 sec 您可以将速度更改为快于或慢于1秒的时间

Working jsFiddle 工作jsFiddle

I think this is the code you need: 我认为这是您需要的代码:

$('.close_this').click(function () {
var position = $("#hide_to_this").position();
$('#hide_box').animate({left:position.left,top:position.top,
                        width:100, height:100},'slow',
                       function(){
                            $('#hide_box').hide();
                       });
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM