有没有办法在CSS中不使用转换就可以得到聊天气泡?

时间:2019-01-30 17:52:39

标签: html css

我目前正在尝试像Ios那样获得聊天气泡。我正在尝试使用-webkit-transform: translate(0, -2px);,但在xulrunner和SWTbrowser之类的旧浏览器中,此css指令无法在那些浏览器上正常工作,尽管它在所有最新的浏览器上均可工作。下面是我的CSS和HTML

body {
  font-family: "Helvetica Neue";
  font-size: 20px;
  font-weight: normal;
}

section {
  max-width: 450px;
  margin: 50px auto;
 
}
 div {
    max-width: 255px;
    word-wrap: break-word;
    margin-bottom: 20px;
    line-height: 24px;
  }

.clear {
  clear: both
}

.from-me {
  position: relative;
  padding: 8px 14px;
  color: #ffffff;
  background: #5ca9db;
  border-radius: 12px;
  float: right;
  margin-left: 56px;
  }
  
  .from-me:before {
  content: "";
  position: absolute;
  bottom: -2px;
  right: -7px;
  height: 20px;
  border-right: 20px solid #5ca9db;
  border-bottom-left-radius: 16px 14px;
  -webkit-transform: translate(0, -2px);
} 
.from-me:after {
  content: "";
  position: absolute;
  bottom: -2px;
  right: -56px;
  width: 26px;
  height: 20px;
  background: #ffffff;
  border-bottom-left-radius: 10px;
  -webkit-transform: translate(-30px, -2px);
}
.from-them {
  position: relative;
  padding: 8px 14px;
  background: #f2f3f5;
  border-radius: 12px;
  float: left;
  margin-right: 56px;
}

.from-them:before {
  content: "";
  position: absolute;
  bottom: -2px;
  left: -7px;
  height: 20px;
  border-left: 20px solid #f2f3f5;
  border-bottom-right-radius: 16px 14px;
  -webkit-transform: translate(0, -2px);
}

.from-them:after {
  content: "";
  position: absolute;
  bottom: -2px;
  left: 4px;
  width: 26px;
  height: 20px;
  background: #ffffff;
  border-bottom-right-radius: 10px;
  -webkit-transform: translate(-30px, -2px);
}
<section>
  <div class="from-me">
    <p>Hey there!What's up?!</p>
  </div>
  <div class="clear"></div>
  
</section>
<section>
  <div class="from-them">
    <p>Hey there!What's up?!</p>
  </div>
  <div class="clear"></div>
  
</section>

尽管在chrome上它可以提供正确的输出,但是在旧的浏览器(例如SWT浏览器和XULrunner)上无法正常输出。在这些浏览器中获得的输出下方。

enter image description here

正如我所说的,-webkit-transform: translate在这些浏览器中无法给出正确的结果。任何没有-webkit-transform: translate的指针都可以达到相同的效果。

2 个答案:

答案 0 :(得分:1)

您可能需要稍微调整外观,但是可以使用没有任何变换的三角形。这是几个例子。

div {
  margin-bottom: 50px;
 }
.bubble1, .bubble2 {
  width: 300px;
  height: 100px;
  border-radius: 10px 10px 0 10px;
  background-color: #5e98d6;
  position: relative;
  }
  
.bubble1:after {
  content:'';
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 15px 15px 0;
  border-color: transparent #5e98d6 transparent transparent;
  position: absolute;
  right:0;
  top:100%;
}

.bubble2:after {
  content:'';
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 15px 0 0 15px;
  border-color: transparent transparent transparent #5e98d6;
  position: absolute;
  left:100%;
  bottom:0;
}
<div class="bubble1"></div>

<div class="bubble2"></div>

答案 1 :(得分:0)

出于您的目的,由于您使用的是absolute定位,因此translate相当于相应的toprightbottom或{{1} }值。只需将转换后的值添加到它们中,您将获得一些好处:

left
body {
  font-family: "Helvetica Neue";
  font-size: 20px;
  font-weight: normal;
}

section {
  max-width: 450px;
  margin: 50px auto;
 
}
 div {
    max-width: 255px;
    word-wrap: break-word;
    margin-bottom: 20px;
    line-height: 24px;
  }

.clear {
  clear: both
}

.from-me {
  position: relative;
  padding: 8px 14px;
  color: #ffffff;
  background: #5ca9db;
  border-radius: 12px;
  float: right;
  margin-left: 56px;
  }
  
  .from-me:before {
  content: "";
  position: absolute;
  bottom: 0;
  right: -7px;
  height: 20px;
  border-right: 20px solid #5ca9db;
  border-bottom-left-radius: 16px 14px;  
} 
.from-me:after {
  content: "";
  position: absolute;
  bottom: 0;
  right: -26px;
  width: 26px;
  height: 20px;
  background: #ffffff;
  border-bottom-left-radius: 10px;
}
.from-them {
  position: relative;
  padding: 8px 14px;
  background: #f2f3f5;
  border-radius: 12px;
  float: left;
  margin-right: 56px;
}

.from-them:before {
  content: "";
  position: absolute;
  bottom: 0;
  left: -7px;
  height: 20px;
  border-left: 20px solid #f2f3f5;
  border-bottom-right-radius: 16px 14px;
}

.from-them:after {
  content: "";
  position: absolute;
  bottom: 0;
  left: -26px;
  width: 26px;
  height: 20px;
  background: #ffffff;
  border-bottom-right-radius: 10px;
}