7

我正在尝试在锚点中垂直居中文本。我使用了 line-height 并且它可以工作,但是当文本换行时,第二行中的文本获得 line-height ......我怎样才能将文本在锚点中居中而不会像那样失败?

运行此代码片段并单击圆圈以显示其他圆圈,您将明白我的意思。

(function($) {
  $.path = {};
  $.path.arc = function(params) {
    for (var i in params) {
      this[i] = params[i]
    }
    this.dir = this.dir || 1;
    while (this.start > this.end && this.dir > 0) {
      this.start -= 360
    }
    while (this.start < this.end && this.dir < 0) {
      this.start += 360
    }
    this.css = function(p) {
      var a = this.start * (p) + this.end * (1 - (p));
      a = a * 3.1415927 / 180;
      var x = Math.sin(a) * this.radius + this.center[0];
      var y = -Math.cos(a) * this.radius + this.center[1];
      return {
        top: y + "px",
        left: x + "px"
      }
    }
  };
  $.fx.step.path = function(fx) {
    var css = fx.end.css(1 - fx.pos);
    for (var i in css) {
      fx.elem.style[i] = css[i]
    }
  }
})(jQuery);

$(function() {
  var clicked = false,
    allowAnimate = true;
  $('.MainCircle').click(function() {
    var list = $(this).siblings('ul').children('li'),
      noli = list.size();
    if ((!clicked) && (allowAnimate)) {
      allowAnimate = false;
      list.each(function(i) {
        var li = $(this);
        var liw = li.width();
        if (i == 0) {
          li.animate({
            top: (liw * -2.3) + "px"
          }, 250);
        } else {
          li.delay(250).animate({
            path: new $.path.arc({
              center: [0, 0],
              radius: liw * 2.3,
              start: 0,
              end: 360 / noli * i,
              dir: 1
            })
          }, 500, function() {
            if (i + 1 == noli) {
              clicked = !clicked;
              allowAnimate = true;
            }
          });
        }
      });
    } else if ((clicked) && (allowAnimate)) {
      allowAnimate = false;
      list.each(function(i) {
        var li = $(this);
        li.animate({
          top: 0,
          left: 0
        }, 500, function() {
          if (i + 1 == noli) {
            clicked = !clicked;
            allowAnimate = true;
          }
        });
      });
    }
  });
});
body {
  font-family: verdana;
  font-size: 12px
}

ul {
  list-style: none;
  margin: 0;
  padding: 0
}

a {
  color: #000;
  text-decoration: none
}

#CircledMenu li {
  position: absolute
}

.MainCircle {
  background: red;
  border-radius: 100px;
  display: block;
  height: 200px;
  line-height: 200px;
  position: absolute;
  text-align: center;
  width: 200px;
  z-index: 1
}

.SubMenu li {
  margin: 50px
}

.SubMenu a {
  background: red;
  border-radius: 50px;
  display: block;
  height: 100px;
  line-height: 100px;
  text-align: center;
  width: 100px
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>test</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>

<body>
  <ul style="margin:200px;" id="CircledMenu">
    <li>
      <a href="#" class="MainCircle">Products</a>
      <ul class="SubMenu">
        <li><a href="#">Kitchen</a></li>
        <li><a href="#">Bedroom</a></li>
        <li><a href="#">Car</a></li>
        <li><a href="#">DIY/Storage</a></li>
        <li><a href="#">Bathroom</a></li>
        <li><a href="#">Footwear</a></li>
        <li><a href="#">Garden/Outdoor/Travel</a></li>
        <li><a href="#">Health</a></li>
        <li><a href="#">Gifts</a></li>
        <li><a href="#">Pet</a></li>
        <li><a href="#">Living Solutions</a></li>
        <li><a href="#">Clock/Lighting</a></li>
        <li><a href="#">Personal Care</a></li>
        <li><a href="#">Practial Solutions</a></li>
      </ul>
    </li>
  </ul>
</body>

</html>

我想支持尽可能多的浏览器。如果您添加额外的属性以支持更多浏览器,请通过添加注释说明哪一行适用于哪个浏览器。

4

2 回答 2

7

您可以display:table为此使用属性。像这样写:

.SubMenu li {
  margin:50px;
  background:red;
  border-radius:50px;
  height:100px;
  width:100px;
  display:table;
}

.SubMenu a {
   height:100px;
   width:100px;
   display:table-cell;
   text-align: center;
   vertical-align: middle;
}

检查这个JSFiddle。

于 2012-03-17T20:24:17.977 回答
0

您可以为此使用元素样式

element.style {
line-height: 13px;
padding-top: 37px;
height: 60px;}
于 2012-04-13T12:06:51.023 回答