javascript - How can I put element in the middle of vertical align? -
i made demo jsfiddle please check.
this show comment slides right side left.
it's shown little bit above middle of vertical align.
how can show right in middle?
please fix , update jsfiddle
javascript
function transition() { $('.newsticker p').animate({"marginleft":"400px","opacity":".0"}, 600).fadeout(100); $('.newsticker').append("<p style='margin-left:400px;opacity:0'>hello! test</p>"); $('.newsticker p').animate({"marginleft":"0px","opacity":"1"}, 600); } setinterval(transition, 2000); css
div.newsticker{ border:1px solid #666666; width:100%; height:100px; } .newsticker p{ padding-left:10px; padding-right:10px; float:left; position:absolute; } html
<div class="newsticker"> </div>
first reset browsers default stylesheet margin or padding by:
* { padding: 0; margin: 0; } then add line-height: 100px; css declaration .newsticker element:
div.newsticker{ border:1px solid #666666; width:100%; height:100px; /* -------- */ line-height: 100px; /* | */ /* ^---------- */ } update
by using css, impossible achieve goal. create jquery version, calculates height of dynamic paragraph , set top property middle of parent. in case little change needed in css:
css:
div.newsticker { position: relative; /* add relative position parent */ overflow: hidden; /* hide overflow */ } .newsticker p { width: 100%; /* set width of paragraph '100%' or 'inherit' */ } javascript/jquery:
var newsticker = $('.newsticker'), maxheight = newsticker.height(); function transition() { newsticker.find('p').animate({ marginleft : "400px", opacity : ".0" }, 600).fadeout(100); newsticker.append( $('<p>').css({ 'margin-left' : '400px', 'opacity' : '0' // put text in .text() method: }).text('lorem ipsum dolor sit amet, consectetur adipisicing elit. ipsam suscipit nihil voluptatibus maxime sit quam delectus eaque officiis cumque accusamus velit nesciunt deserunt veniam molestias alias? eaque iste quia non.') ).find('p').each(function() { if ($(this).css('top') == 'auto') $(this).css('top', (maxheight - $(this).height()) / 2 ); }); newsticker.find('p').animate({"marginleft":"0px","opacity":"1"}, 600); } setinterval(transition, 2000); here jsfiddle demo.
Comments
Post a Comment