垂直对齐JQuery插件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了垂直对齐JQuery插件相关的知识,希望对你有一定的参考价值。
Learn how to place a DIV in the exact middle of the screen using this JQuery horizontal & vertical align plugin.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>Demo</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script> <script type="text/javascript"> (function ($) { $.fn.vAlign = function() { return this.each(function(i){ var h = $(this).height(); var oh = $(this).outerHeight(); var mt = (h + (oh - h)) / 2; $(this).css("margin-top", "-" + mt + "px"); $(this).css("top", "50%"); $(this).css("position", "absolute"); }); }; })(jQuery); (function ($) { $.fn.hAlign = function() { return this.each(function(i){ var w = $(this).width(); var ow = $(this).outerWidth(); var ml = (w + (ow - w)) / 2; $(this).css("margin-left", "-" + ml + "px"); $(this).css("left", "50%"); $(this).css("position", "absolute"); }); }; })(jQuery); $(document).ready(function() { $("#content").vAlign(); $("#content").hAlign(); }); </script> <style type="text/css"> html { background: #fafafa; } #content { background: #fff; border: 10px solid #eee; padding: 20px; color: #666; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 25px; text-align: justify; } #content { width: 400px; } </style> </head> <body> <div id="content"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas eu dui eget nulla condimentum gravida. Vivamus erat leo, ultricies quis, gravida a, fringilla eu, urna. Pellentesque a mauris ac nisl semper egestas. Pellentesque ut elit in pede mattis gravida. Donec ac lectus a nisi suscipit placerat. Maecenas quis ipsum. Pellentesque mattis tellus. Suspendisse sollicitudin accumsan tortor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed metus. Quisque et leo at erat rutrum lobortis. In tempus lectus eget ligula convallis tristique. </div> </body> </html>
以上是关于垂直对齐JQuery插件的主要内容,如果未能解决你的问题,请参考以下文章