【问题标题】:Center texts inside circles & fadeToggle not working在圆圈内居中文本和淡入淡出切换不起作用
【发布时间】:2019-01-07 15:17:10
【问题描述】:

这是我在 StackOverflow 中的第一个问题。所以,如果我没有遵守发布问题的规则,请告诉我。 :)

[问题1]

我试图通过阅读以下帖子来解决我的文本以圆圈为中心的问题:

  1. Center text and container inside a circle
  2. SVG center text in circle

但是,我无法解决我的问题。我想将圆圈中的文本移动到圆圈的中间,以使文本水平和垂直位于圆圈的中心。

[问题2]

点击“测试”按钮后会出现隐藏的圆圈。但是,再次单击按钮后圆圈不会消失。

如果有人能给我建议,我将不胜感激! :)

解决了!

  • 这个问题不是重复的,因为我想在使用 translate() 时使文本居中,但对在“fadeToggle()”和“:hidden with fadeIn()”之间使用感到困惑。

$(document).ready(function() {
  $('#test').click(function() {
    $(".options:hidden").fadeToggle();
  });
});
body {
  font-family: 'Poor Story', sans-serif;
}

#test {
  cursor: pointer;
  display: block;
  text-align: center;
  position: absolute;
  display: flex;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.options {
  background: #f7f7f5;
  display: none;
  text-align: center;
  vertical-align: middle;
  position: absolute;
  width: 100%;
  left: 50%;
  top: 50%;
  border-radius: 50%;
  border-style: solid;
  border-color: #F3C78D;
  width: 60px;
  height: 60px;
  font-size: 12px;
}

.options span {
  color: #000000;
  text-align: center;
  vertical-align: middle;
  position: absolute;
  width: 100%;
}

#option1 {
  transform: translate(-100%, -150%);
}

#option2 {
  transform: translate(-160%, -40%);
}

#option3 {
  transform: translate(-50%, 50%);
}

#option4 {
  transform: translate(60%, -40%);
}

#option5 {
  transform: translate(15%, -150%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap 4.1.x -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
  <link href="https://fonts.googleapis.com/css?family=Poor+Story" rel="stylesheet">

</head>

<body>
  <div class="container">
    <button type="button" class="btn btn-outline-success" id="test">test</button>
    <div class="options" id="option1"><span>Hello<br>World</span></div>
    <div class="options" id="option2"><span>Goodbye</span></div>
    <div class="options" id="option3"><span>How<br>are<br>you?</span></div>
    <div class="options" id="option4"><span>Fine</span></div>
    <div class="options" id="option5"><span>Okay</span></div>
  </div>
</body>
<footer>
  <!-- Bootstrap 4.0 : jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>
  <script type="text/javascript" src="index.js"></script>
</footer>

</html>

【问题讨论】:

标签: javascript jquery html css


【解决方案1】:

$(document).ready(function() {
	$('#test').click(function(){
		$(".options").fadeToggle();
	});
});
body{
  font-family: 'Poor Story', sans-serif;
}

#test{
   cursor: pointer;
   display: block;
   text-align: center;
   position: absolute;
   display: flex;
   left: 50%;
   top: 50%; 
   transform: translate(-50%, -50%);
}
.options {
    background: #f7f7f5;
    display: table;
    text-align: center;
    vertical-align: middle;
    position: absolute;
    width: 100%;
    left: 50%;
    top: 50%; 
    border-radius: 50%;
    border-style: solid;
    border-color: #F3C78D;
    width: 60px;
    height: 60px;
    font-size: 12px;
}

.options span {
    display: table-cell;
    color: #000000;
    vertical-align: middle;   
}

#option1{
    transform: translate(-100%, -150%);
}

#option2{
    transform: translate(-160%, -40%);
}

#option3{
    transform: translate(-50%, 50%);
}

#option4{
    transform: translate(60%, -40%);
}

#option5{
    transform: translate(15%, -150%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap 4.1.x -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
  <link href="https://fonts.googleapis.com/css?family=Poor+Story" rel="stylesheet">

</head>
<body>
  <div class="container">
    <button type="button" class="btn btn-outline-success" id="test">test</button>
    <div class="options" id="option1"><span>Hello<br>World</span></div>
    <div class="options" id="option2"><span>Goodbye</span></div>
    <div class="options" id="option3"><span>How<br>are<br>you?</span></div>
    <div class="options" id="option4"><span>Fine</span></div>
    <div class="options" id="option5"><span>Okay</span></div>
  </div>
</body>
<footer>
  <!-- Bootstrap 4.0 : jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>
  <script type="text/javascript" src="index.js"></script>
</footer>
</html>

【讨论】:

    【解决方案2】:

    添加此 CSS 代码

    top: 50%; 
    left: 50%; 
    transform: translateX(-50%) translateY(-50%);
    

    .options span 使文本居中,问题1。

    从 jquery 语句中删除 :hidden 以解决您的问题 2。

    $(document).ready(function() {
      $('#test').click(function() {
        $(".options").fadeToggle();
      });
    });
    body {
      font-family: 'Poor Story', sans-serif;
    }
    
    #test {
      cursor: pointer;
      display: block;
      text-align: center;
      position: absolute;
      display: flex;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }
    
    .options {
      background: #f7f7f5;
      display: none;
      text-align: center;
      vertical-align: middle;
      position: absolute;
      width: 100%;
      left: 50%;
      top: 50%;
      border-radius: 50%;
      border-style: solid;
      border-color: #F3C78D;
      width: 60px;
      height: 60px;
      font-size: 12px;
    }
    
    .options span {
      color: #000000;
      text-align: center;
      vertical-align: middle;
      position: absolute;
      width: 100%;
      top: 50%; 
      left: 50%; 
      transform: translateX(-50%) translateY(-50%); 
    }
    
    #option1 {
      transform: translate(-100%, -150%);
    }
    
    #option2 {
      transform: translate(-160%, -40%);
    }
    
    #option3 {
      transform: translate(-50%, 50%);
    }
    
    #option4 {
      transform: translate(60%, -40%);
    }
    
    #option5 {
      transform: translate(15%, -150%);
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    
      <!-- Bootstrap 4.1.x -->
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
      <link href="https://fonts.googleapis.com/css?family=Poor+Story" rel="stylesheet">
    
    </head>
    
    <body>
      <div class="container">
        <button type="button" class="btn btn-outline-success" id="test">test</button>
        <div class="options" id="option1"><span>Hello<br>World</span></div>
        <div class="options" id="option2"><span>Goodbye</span></div>
        <div class="options" id="option3"><span>How<br>are<br>you?</span></div>
        <div class="options" id="option4"><span>Fine</span></div>
        <div class="options" id="option5"><span>Okay</span></div>
      </div>
    </body>
    <footer>
      <!-- Bootstrap 4.0 : jQuery first, then Popper.js, then Bootstrap JS -->
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>
      <script type="text/javascript" src="index.js"></script>
    </footer>
    
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      • 2011-09-10
      • 1970-01-01
      相关资源
      最近更新 更多