开发者

JavaScript moving slightly down a Message (div)?

Is there a way to move down 开发者_运维知识库by some pixel a div with a text inside? (Maybe using jQuery or w/e)

The effect I would get is like when stackoverflow shows at top the yellow message (for a badge) But I need it inside a page, without moving down all the rest of the page

EXAMPLE:

http://img690.imageshack.us/img690/7324/senzatitolo2mb.jpg

(I would add a fade effect too while the message is moving down)

Ps. Please consider the message can be more than 1 (just like stackoverflow at top)


with jQuery this would be done like:

<div id="message">Some message</div>
$("#message").slideDown(500); //where 500 is the time effect in miliseconds..

Online demo: http://jsfiddle.net/NzPfM/

Se more about jQuery effects here: http://api.jquery.com/category/effects/

If you want to slide it down and fade it in at the same time, then you should use .animate() instead, something like:

$("#message").animate({height:"30px", opacity:1 },500);

Online demo: http://jsfiddle.net/NzPfM/1/

UPDATE: If you want to avoid moving other content while animating you can use position:absolute in css see demo below:

Demo avoiding push down: http://jsfiddle.net/NzPfM/2/


You can set the div to position:absolute and then animate it down using jQuery.animate to change the top style.

read about jQuery.animate here: http://api.jquery.com/animate/

You can see a simple example here: http://jsfiddle.net/NsxTa/

Note: This method as opposed to using the slideDown will actually slide the entire div down from it's hiding place, where as slideDown will just reveal statically positioned content, which imo looks really awefull


Assuming that your page is not laying inside some container with position-absolute, adding a container element as fist child of the body will push down render all the rest of the page. (container - any HTML tag that contains HTML. usually DIV).

This is an example using pure javascript:

http://jsfiddle.net/osher/ByngB/

connect the "add" to your messaging event, or render the on the server connect the "remove" to the close button of the message bar and that will be all :)

function $e(s){ return document.getElementById(s) }

function add(){
    var d = document.createElement("div");
    d.innerHTML = "your message: " + $e("txt").value;
    document.body.insertBefore(d, document.body.firstChild);
}

function remove(){
    // assuming that your page content is wrapped in a div with ID="content"
    if (document.body.firstChild.id == "content") return;

    document.body.removeChild( document.body.firstChild);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜