简体   繁体   English

CSS变换偏移量随文本长度而变化

[英]CSS Transform offset varies with text length

I have set up a demo that has 5 floating <div> s with rotated text of varying length. 我已经设置了一个带有5个浮动<div>演示 ,其中旋转的文本长度不一。 I am wondering if there is a way to have a CSS class that can handle centering of all text regardless of length. 我想知道是否有办法让CSS类可以处理所有文本的居中,无论长度如何。 At the moment I have to create a class for each length of characters in my stylesheet. 目前,我必须为样式表中的每个字符长度创建一个类。 This could get too messy. 这可能会变得太乱。 I have also noticed that the offsets get screwd up is I increase or decrease the size of the wrapping <div> . 我还注意到,当我增加或减少包装<div>的大小时,偏移被搞砸了。

I will be adding these classes to divs through jQuery, but I still have to set up each class for cross-browser compatibility. 我将通过jQuery将这些类添加到div中,但我仍然需要设置每个类以实现跨浏览器兼容性。

.transform3 {
    -webkit-transform-origin: 65% 100%;
    -moz-transform-origin: 65% 100%;
    -ms-transform-origin: 65% 100%;
    -o-transform-origin: 65% 100%;
    transform-origin: 65% 100%;    
}
.transform4 {
    -webkit-transform-origin: 70% 110%;
    -moz-transform-origin: 70% 110%;
    -ms-transform-origin: 70% 110%;
    -o-transform-origin: 70% 110%;
    transform-origin: 70% 110%;
}
.transform5 {
    -webkit-transform-origin: 80% 120%;
    -moz-transform-origin: 80% 120%;
    -ms-transform-origin: 80% 120%;
    -o-transform-origin: 80% 120%;
    transform-origin: 80% 120%;
}
.transform6 {
    -webkit-transform-origin: 85% 136%;
    -moz-transform-origin: 85% 136%;
    -ms-transform-origin: 85% 136%;
    -o-transform-origin: 85% 136%;
    transform-origin: 85% 136%;
}
.transform7 {
    -webkit-transform-origin: 90% 150%;
    -moz-transform-origin: 90% 150%;
    -ms-transform-origin: 90% 150%;
    -o-transform-origin: 90% 150%;
    transform-origin: 90% 150%;
}

Note: The offset values I set were eye-balled. 注意:我设置的偏移值是眼球。


Update 更新

Although I would like this handled with a stylesheet, I believe that I will have to calculate the transformations of the CSS in JavaScript. 虽然我希望用样式表来处理,但我相信我必须在JavaScript中计算CSS的转换。

I have created the following demo to demonstrate dynamic transformations. 我创建了以下演示来演示动态转换。 In this demo, the user can adjust the font-size of the .v_text class and as long as the length of the text does not exceed the .v_text_wrapper height, the text should be vertically aligned in the center, but be aware that I have to adjust the magicOffset value. 在这个演示中,用户可以调整.v_text类的font-size ,只要文本的长度不超过.v_text_wrapper高度,文本应该在中心垂直对齐,但请注意我有调整magicOffset值。

Well, I just noticed that this does not work in iOS... Thanks @Dinesh Kumar DJ 好吧,我只是注意到这在iOS中不起作用... 谢谢@Dinesh Kumar DJ

Here you go: http://codepen.io/teodragovic/pen/fgekh 你去: http//codepen.io/teodragovic/pen/fgekh

#output1,
#output2{
  margin: 50px;
  display: inline-block;
}

.rotate {
  //remove this line if using js
  transform: translateY(150px) rotate(-90deg);
}

.v_text_wrapper {
  float: left;
  width: 100px;
  height: 150px;
  background: #AAA;
  border: solid #222 1px;
  padding: 0;
  margin: 0;
  position: relative;
}

#output2 .v_text_wrapper {
  height: 170px;
}

.v_text {
  color: #444;
  font-family: monospace;
  font-weight: bold;
  font-size: 1em;

  width: 150px; //remove this line if using js
  height: 100px;
  transform-origin: top left;
  text-align: center;
  position: absolute;
  display: table;
}

p {
  display: table-cell;
  vertical-align: middle;
}

No need for JS and dynamic offseting. 不需要JS和动态的offseting。 You can set all <div> 's containing text at same width and height as wrappers, rotate them around top left corner (could be any corner) and then use translateY to position them back inside wrapper (this assumes that wrapper dimensions are always the same and known value). 您可以将包含文本的所有<div>设置为与包装器相同的宽度和高度,将它们围绕左上角(可以是任何角落)旋转,然后使用translateY将它们放回到包装器内(这假设包装器尺寸始终为相同且已知的价值)。

I added extra <p> element around text and did vertical centering using this method: http://css-tricks.com/vertically-center-multi-lined-text/ 我在文本周围添加了额外的<p>元素,并使用此方法进行垂直居中: http//css-tricks.com/vertically-center-multi-lined-text/

Edit: I updated pen in case wrapper <div> changes size. 编辑:我更新了笔,以防包装<div>更改大小。 Since <div> 's are rotated, their witdh becames height and vice versa so if wrapper has dimension 100x150 then child has to be 150x100. 由于<div>是旋转的,因此它们的高度相反,反之亦然,所以如果包装的尺寸为100x150,那么子必须为150x100。

$('.v_text_wrapper').each(function(){
  var x = $(this).css("height");
  var text = $(this).children('.v_text');
  text.css({"transform":"translateY("+x+") rotate(-90deg)", "width":x});
});

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

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