简体   繁体   English

如何解决 alignment 文本中心问题以及 hover 上的其他元素

[英]How to fix alignment issue of text center in addition to other element on hover

I have some ul li element.我有一些 ul li 元素。 Here on hover of each element one new element is adding on the right side of the text.这里在每个元素的 hover 上,一个新元素被添加到文本的右侧。 The alignment of the text is center but on hover when a new element is added on right side of it its alignment is getting changed.文本的 alignment 位于中心,但在 hover 上,当在其右侧添加新元素时,其 alignment 正在发生变化。 My problem is here, Alignment of text/new element should not effect on adding new element on hover. Here is the code below https://stackblitz.com/edit/angular-emvs2v?file=src%2Fstyles.css我的问题在这里,文本/新元素的 Alignment 不应该影响在 hover 上添加新元素。这是下面的代码https://stackblitz.com/edit/angular-emvs2v?file=src%2Fstyles.css

home.component.html home.component.html

<div style="width:50%" class="text-center box">
  <ul>
    <li [class.btn-success]="selectedIndex === 1" (mouseout)="removeIndex(1)"
        (mouseover)="setIndex(1)">Home <span class="float-right test">==></span>
    </li>
    <li [class.btn-success]="selectedIndex === 2" (mouseout)="removeIndex(2)"
        (mouseover)="setIndex(2)">Contact Us <span
      class="float-right test">==></span></li>
    <li [class.btn-success]="selectedIndex === 3" (mouseout)="removeIndex(3)"
        (mouseover)="setIndex(3)">Production<span
      class="float-right test">==></span></li>
  </ul>
</div>
<button (click)="getSelect()">Submit</button>

home.component.ts主页.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})

export class HomeComponent implements OnInit {
  data: any;
  selectedIndex = 1;

  constructor () { }

  ngOnInit () {

  }

  setIndex (index: number) {
    this.selectedIndex = index;
  }

  removeIndex (index: number) {
    this.selectedIndex = null;
  }

  getSelect () {
    this.selectedIndex = 1;
  }
}

css css

ul li {
  list-style-type: none;
}

.test {
  display: none;
}

.btn-success .test {
  display: block;
}

.text-center {
  text-align: center;
}

.float-right {
  float: light;
}

You need to take the new element outside of the usual flow, eg using position: absolute;您需要将新元素带出常规流程,例如使用position: absolute; Change your CSS to:将您的 CSS 更改为:

ul li{
  list-style-type: none;
}
.test{
  display: none;    
}
.btn-success .test{
  display: block;
}
.text-center{
  text-align: center;
}
li {
  position: relative;
}
.float-right{
  position: absolute;
  right: 0;
  top: 0;
}

You can check the working example here: https://angular-kcnrcg.stackblitz.io您可以在此处查看工作示例: https://angular-kcnrcg.stackblitz.io

By using display:none and display:block , you are removing the item from the DOM, causing surrounding items to move.通过使用display:nonedisplay:block ,您可以从 DOM 中移除该项目,从而导致周围的项目移动。

You can fix this by using opacity:0 and opacity:1 , instead.您可以改用opacity:0opacity:1来解决此问题。

You can also use the :hover pseudo-class to greatly simply the code.您还可以使用:hover伪类来大大简化代码。

 ul li { list-style-type: none; }.test { opacity: 0; float: right; }.btn-success:hover.test { opacity: 1; }
 <div style="width:150px;"> <ul> <li class="btn-success">Home <span class="test">==></span></li> <li class="btn-success">Contact Us <span class="test">==></span></li> <li class="btn-success" >Production<span class="test">==></span></li> </ul> </div>

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

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