溢出滚动不适用于班级孩子

时间:2018-08-06 21:39:10

标签: html css overflow

我尝试仅为文本添加overflow:scroll,文本被类element-text包围,但不起作用,什么也没发生。我在父类(element)中具有相对位置,而在子类(element-text)中具有绝对位置。这是我的代码codepen

HTML:

        <div class="person-box">
            <a href="{{ url('#') }}"><div class="person-photo-autor"></div></a>
            <div class="person-text">
                <div class="person-name">
                    <a href="{{ url('#') }}">Sylvester Stallone</a>
                </div>
                <div class="person-workplace">
                    Web & mobile apps | ML & DL | Software | Stermedia | People | HR
                </div>

                <div class="slider-position">
                    <div class="elements-list" id="slider1">
                        <div class="element">
                            <div class="element-text">
                                Sylvester is a person of many talents. He's a great leader, coach and motivational speaker. Sylvester is always goal-oriented, very focused and patient person, which assures his recruiting services to be on a very high level. It was a great luck to have Sylvester as a team leader in our team.
                                Sylvester is a person of many talents. He's a great leader, coach and motivational speaker. Sylvester is always goal-oriented, very focused and patient person, which assures his recruiting services to be on a very high level. It was a great luck to have Sylvester as a team leader in our team.
                            </div>
                        </div>
                        <div class="element">
                            <div class="element-text">
                                2 Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsum exercitationem asperiores perspiciatis sapiente ducimus atque
                                dicta labore dolores sequi ut.
                            </div>
                        </div>
                        <div class="element">
                            <div class="element-text">
                                3 Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus id quas, quia consectetur repellat quis! Minus voluptatibus,
                                dolores iure deserunt.
                            </div>
                        </div>
                    </div>
                </div>

            </div>
            <div class="person-author">
                <div class="person-quotation">
                    <span></span>
                </div>
                <div class="person-author-date">
                    <a href="{{ url('#') }}" class="person-author-name-href">
                    <div class="person-author-name">Jan Kowalski</div>
                    <div class="person-author-workplace">Programista Java</div>
                    </a>
                </div>
            </div>
        </div>

CSS:

.person-box {
    position: relative;
    margin: 80px 5px;
    border: 2px solid rgb(202, 202, 202);
    font-family: Yanone Kaffeesatz;
    max-width: 100%;
}

.person-photo-autor {
    position: absolute;
    left: 44%;
    top: -48px;
}

.person-photo-autor::before {
    content:"";    
    position: absolute;
    border-radius: 50%;
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    background-image: url('../photo_linkedin/mc.jpg');
    background-repeat: no-repeat;
    background-size: 80px 80px;
    width: 80px;
    height: 80px;
    box-shadow: 0 0 0 4px #afafaf;
}

.person-workplace {
    font-size: 18px;
    color: #909090;
    padding: 7px 0;
}

/* .person-date {
    font-style: italic;
    color: #868686;
    padding-top: 10px;
} */

/* .person-description {
    font-size: 23px;
    margin: 5px 15px 0 15px;
} */

.person-text {
    padding: 20px;
    margin-top: 30px;
    /* height: 200px;
    overflow: hidden; */
}

.person-name {
    text-align: center;
    font-size: 22px;
    font-weight: 800;
}

.person-name a {
    color: #6d6d6d;
}

.person-name a:hover {
    color: #af5f5f;
}

.person-author {
    border-top: 2px solid rgb(202, 202, 202);
    display: table;
    table-layout: fixed;
    width: 100%;
    height: 100%;
}

.person-quotation {
    display: table-cell;
    height: 100%;
    vertical-align: middle;
    width: 15%;
    border-right: 2px solid #dadada;
    box-sizing: border-box;
}

.person-quotation span {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 10px 0;
}

.person-author-date {
    position: relative;
    text-align: center;
    max-height: 100%;
}

.person-author-name-href {
    color:#5f535c;
}

.person-author-name-href:hover {
    color:#a54e8f;
}

.person-author-name, .person-author-workplace {
    position: absolute;
    top: 8px;
    text-align: center;
    width: 100%;
    font-size: 18px;
}

.person-author-workplace {
    top: 25px;
}

.person-quotation span::before {
    content: url('../img/quotations.png');
}

.slider-position {
    position: relative;
    height: 200px;
}

.slider {
    position: relative;
    background-position: center center;
    background-size: cover;
}

.slider-slides-cnt {
    width: 100%;
    max-height: 100%;
    position: relative;
}

.slider-slide {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    /* justify-content: center;
    display: flex;
    flex-flow: column;
    align-items: center; */
    /* opacity: 0; */
    z-index: 0;
}

.slider-slide-active {
    z-index: 1;
    opacity: 1;
    left: 0;
    opacity: 1;
}


.slider-slide-active .element-text {
    left: 0;
    opacity: 1;
}

.element {
    position: relative;
}

.element-text {
    position: absolute;
    color: rgb(78, 78, 78);
    left: -3rem;
    opacity: 0;
    padding: 5px 40px 0 40px;
    font-size: 1.5rem;
    font-family: Yanone Kaffeesatz;
    overflow:scroll;

}

.slider-nav {
    position: relative;
}

.slider-button-prev, .slider-button-next {
    position: absolute;
    cursor: pointer;
    z-index: 2;
    left:0;
    top: 0;
    transform: translateY(300%);
    width:1.5rem;
    height:1.5rem;
    border-radius: 5%;
    background: transparent;
    border: 0;
    text-indent:-999px;
    overflow:hidden;
}

.slider-button-next {
    left: auto;
    right: 0;
}

.slider-button-prev:focus, .slider-button-next:focus {
    outline: none;
}

.slider-button-prev::before, .slider-button-next::before {
    content:'<';
    position: absolute;
    display: flex;
    justify-content: center;
    align-items: center;
    left:0;
    top:0;
    width:100%;
    height:100%;
    font-size:1.3rem;
    color:rgb(255, 188, 0);
    text-indent: 0;
    transition: all .4s ease-in-out;
}

.slider-button-next::before {
    content: '>';
}

.slider-button-prev:hover::before, .slider-button-next:hover::before {
    color:black;
    transform: scale(1.3);
}

.slider-dots {
    z-index: 2;
    position: relative;
    left: 0;
    margin: 0;
    padding:0;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    list-style: none;
    top:200px;
}

.slider-dots-button {
    background: transparent;
    width:1rem;
    height:1rem;
    border:0;
    text-indent:-999px;
    overflow: hidden;
    cursor: pointer;
    position: relative;
}

.slider-dots-button::before {
    content:'';
    position: absolute;
    left:100%;
    top:100%;
    transform:translate(-100%, -100%);
    background: transparent;
    width:0.5rem;
    height:0.5rem;
    border-radius: 50%;
    transition:0.5s all;
    box-shadow:inset 0 0 0 2px rgb(191, 191, 191);
}

.slider-dots-element-active .slider-dots-button::before {
    background: rgb(191, 191, 191);
    transform: translate(-100%, -100%);
}

.slider-dots-button:focus {
    outline: none;
}

/* animacja */

.slider-style {
    opacity: 0;
    transition: .4s all;
}

.slider .element-date-animate {
transition: .3s ease;
}

.slider .element-text {
transition: .3s ease;
}

/* @keyframes load {
  from{
    width: 0;
  }
  to{
    width: 90%;
  }
}

@-webkit-keyframes load {
  from{
    width: 0;
  }
  to{
    width: 90%;
  }
} */

.slider-slide-active {
    opacity: 1;
}
.slider-slide-active {
    left:0;
    opacity:1;
}
.slider-slide-active .element-text {
    left:0;
    opacity:1;
}

3 个答案:

答案 0 :(得分:1)

我将所有容器调整为absolute,并从top:0移至bottom:0,从left:0移至right:0,以便它们都具有确切的尺寸/尺寸。

.element-text overflow-y设置为auto,因此在需要时显示scrollBar。

//Slider
class Slider {
  constructor(elemSelector, opts) {

    const defaultOptions = {
      pauseTime: 0,
      nextText: "następny slide",
      prevText: "poprzedni slide",
      generateDots: true,
      generatePrevNext: true
    };

    this.options = Object.assign({}, defaultOptions, opts);

    this.currentSlide = 0;
    this.sliderSelector = elemSelector;
    this.elem = null;
    this.slider = null;
    this.slides = null;
    this.prev = null;
    this.next = null;
    this.dots = [];

    this.generateSlider();
    this.changeSlide(this.currentSlide);
  }

  changeSlide(index) {

    //slide
    [].forEach.call(this.slides, function(slide) {
      slide.classList.remove('element-date-animate');
      slide.classList.remove('slider-slide-active');
      slide.setAttribute('arian-hidden', true);
    });

    this.slides[index].classList.add('element-date-animate');
    this.slides[index].classList.add('slider-slide-active');
    this.slides[index].setAttribute('aria-hidden', false);

    //dots
    if (this.options.generateDots) {

      this.dots.forEach(el => {
        el.classList.remove('slider-dots-element-active');
      });
      this.dots[index].classList.add('slider-dots-element-active');

    }

    this.currentSlide = index;


    if (typeof this.options.pauseTime === 'number' && this.options.pauseTime !== 0) {

      clearInterval(this.time);
      this.time = setTimeout(function() {
        this.slideNext();
      }.bind(this), this.options.pauseTime);

    }

  }

  createDots() {
    const ulDots = document.createElement('ul');
    ulDots.classList.add('slider-dots');
    ulDots.setAttribute('arial-label', 'Slider pagination');

    // console.log();

    for (let i = 0; i < this.slides.length; i++) {
      const li = document.createElement('li');
      li.classList.add('slider-dots-element');

      const btn = document.createElement('button');
      btn.classList.add('slider-dots-button');
      btn.type = 'button';
      btn.innerText = i + 1;
      btn.setAttribute('arial-label', 'Set slide' + (i + 1));

      btn.addEventListener("click", () => {
        this.changeSlide(i);
      });

      li.appendChild(btn);
      ulDots.appendChild(li);

      this.dots.push(li);


    }

    this.slider.appendChild(ulDots);
  }

  slidePrev() {
    this.currentSlide--;
    if (this.currentSlide < 0) {
      this.currentSlide = this.slides.length - 1;
    }
    this.changeSlide(this.currentSlide);
  }

  slideNext() {
    this.currentSlide++;
    if (this.currentSlide > this.slides.length - 1) {
      this.currentSlide = 0;
    }
    this.changeSlide(this.currentSlide);
  }

  createPrevNext() {
    this.prev = document.createElement('button');
    this.prev.type = 'button';
    this.prev.innerText = this.options.prevText;
    let classToAdd = ['slider-button', 'slider-button-prev'];
    this.prev.classList.add(...classToAdd);
    this.prev.addEventListener("click", this.slidePrev.bind(this));

    this.next = document.createElement('button');
    this.next.type = 'button';
    this.next.innerText = this.options.nextText;
    let classToAdd2 = ['slider-button', 'slider-button-next'];
    this.next.classList.add(...classToAdd2);
    this.next.addEventListener("click", this.slideNext.bind(this));

    const nav = document.createElement('div');
    nav.classList.add('slider-nav');
    nav.setAttribute('arial-label', 'Slider prev next');
    nav.appendChild(this.prev);
    nav.appendChild(this.next);
    this.slider.appendChild(nav);
  }

  generateSlider() {

    this.slider = document.querySelector(this.sliderSelector);
    this.slider.classList.add('slider');

    const slidesCnt = document.createElement('div');
    slidesCnt.classList.add('slider-slides-cnt');

    this.slides = this.slider.children;

    while (this.slides.length) {
      this.slides[0].classList.add('slider-slide');
      slidesCnt.appendChild(this.slides[0]);
    }

    this.slides = slidesCnt.children;
    this.slider.appendChild(slidesCnt);

    if (this.options.generatePrevNext) this.createPrevNext();
    if (this.options.generateDots) this.createDots();

  }

}

const optAll = {
  pauseTime: 0,
  prevText: "Poprzedni",
  nextText: "Następny",
  generateDots: true,
  generatePrevNext: true
};
const slider1 = new Slider('#slider1', optAll);
.person-box {
  position: relative;
  margin: 80px 5px;
  border: 2px solid rgb(202, 202, 202);
  font-family: Yanone Kaffeesatz;
  max-width: 100%;
}

.person-photo-autor {
  position: absolute;
  left: 44%;
  top: -48px;
}

.person-photo-autor::before {
  content: "";
  position: absolute;
  border-radius: 50%;
  -webkit-border-radius: 50%;
  -moz-border-radius: 50%;
  background-image: url('../photo_linkedin/mc.jpg');
  background-repeat: no-repeat;
  background-size: 80px 80px;
  width: 80px;
  height: 80px;
  box-shadow: 0 0 0 4px #afafaf;
}

.person-workplace {
  font-size: 18px;
  color: #909090;
  padding: 7px 0;
}


/* .person-date {
    font-style: italic;
    color: #868686;
    padding-top: 10px;
} */


/* .person-description {
    font-size: 23px;
    margin: 5px 15px 0 15px;
} */

.person-text {
  padding: 20px;
  margin-top: 30px;
  /* height: 200px;
    overflow: hidden; */
}

.person-name {
  text-align: center;
  font-size: 22px;
  font-weight: 800;
}

.person-name a {
  color: #6d6d6d;
}

.person-name a:hover {
  color: #af5f5f;
}

.person-author {
  border-top: 2px solid rgb(202, 202, 202);
  display: table;
  table-layout: fixed;
  width: 100%;
  height: 100%;
}

.person-quotation {
  display: table-cell;
  height: 100%;
  vertical-align: middle;
  width: 15%;
  border-right: 2px solid #dadada;
  box-sizing: border-box;
}

.person-quotation span {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 10px 0;
}

.person-author-date {
  position: relative;
  text-align: center;
  max-height: 100%;
}

.person-author-name-href {
  color: #5f535c;
}

.person-author-name-href:hover {
  color: #a54e8f;
}

.person-author-name,
.person-author-workplace {
  position: absolute;
  top: 8px;
  text-align: center;
  width: 100%;
  font-size: 18px;
}

.person-author-workplace {
  top: 25px;
}

.person-quotation span::before {
  content: url('../img/quotations.png');
}


/*slider*/

.slider-position {
  position: relative;
  height: 200px;
}

.slider {
  background-position: center center;
  background-size: cover;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
}

.slider-slides-cnt {
  width: 100%;
  max-height: 100%;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

.slider-slide {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  /* justify-content: center;
    display: flex;
    flex-flow: column;
    align-items: center; */
  /* opacity: 0; */
  z-index: 0;
  bottom: 0;
}

.slider-slide-active {
  z-index: 1;
  opacity: 1;
  left: 0;
  opacity: 1;
}

.slider-slide-active .element-text {
  left: 0;
  opacity: 1;
}

.element {
  position: absolute;
}

.element-text {
  position: absolute;
  color: rgb(78, 78, 78);
  left: -3rem;
  opacity: 0;
  padding: 5px 40px 0 40px;
  font-size: 1.5rem;
  font-family: Yanone Kaffeesatz;
  overflow-y: auto;
  bottom: 0;
  top: 0;
}

.slider-nav {
  position: relative;
}

.slider-button-prev,
.slider-button-next {
  position: absolute;
  cursor: pointer;
  z-index: 2;
  left: 0;
  top: 0;
  transform: translateY(300%);
  width: 1.5rem;
  height: 1.5rem;
  border-radius: 5%;
  background: transparent;
  border: 0;
  text-indent: -999px;
  overflow: hidden;
}

.slider-button-next {
  left: auto;
  right: 0;
}

.slider-button-prev:focus,
.slider-button-next:focus {
  outline: none;
}

.slider-button-prev::before,
.slider-button-next::before {
  content: '<';
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  font-size: 1.3rem;
  color: rgb(255, 188, 0);
  text-indent: 0;
  transition: all .4s ease-in-out;
}

.slider-button-next::before {
  content: '>';
}

.slider-button-prev:hover::before,
.slider-button-next:hover::before {
  color: black;
  transform: scale(1.3);
}

.slider-dots {
  z-index: 2;
  position: relative;
  left: 0;
  margin: 0;
  padding: 0;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  list-style: none;
  top: 200px;
}

.slider-dots-button {
  background: transparent;
  width: 1rem;
  height: 1rem;
  border: 0;
  text-indent: -999px;
  overflow: hidden;
  cursor: pointer;
  position: relative;
}

.slider-dots-button::before {
  content: '';
  position: absolute;
  left: 100%;
  top: 100%;
  transform: translate(-100%, -100%);
  background: transparent;
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 50%;
  transition: 0.5s all;
  box-shadow: inset 0 0 0 2px rgb(191, 191, 191);
}

.slider-dots-element-active .slider-dots-button::before {
  background: rgb(191, 191, 191);
  transform: translate(-100%, -100%);
}

.slider-dots-button:focus {
  outline: none;
}


/* animacja */

.slider-style {
  opacity: 0;
  transition: .4s all;
}

.slider .element-date-animate {
  transition: .3s ease;
}

.slider .element-text {
  transition: .3s ease;
}


/* @keyframes load {
  from{
    width: 0;
  }
  to{
    width: 90%;
  }
}
            
@-webkit-keyframes load {
  from{
    width: 0;
  }
  to{
    width: 90%;
  }
} */

.slider-slide-active {
  opacity: 1;
}

.slider-slide-active {
  left: 0;
  opacity: 1;
}

.slider-slide-active .element-text {
  left: 0;
  opacity: 1;
}
<div class="person-box">
  <a href="{{ url('#') }}">
    <div class="person-photo-autor"></div>
  </a>
  <div class="person-text">
    <div class="person-name">
      <a href="{{ url('#') }}">Sylvester Stallone</a>
    </div>
    <div class="person-workplace">
      Web & mobile apps | ML & DL | Software | Stermedia | People | HR
    </div>

    <div class="slider-position">
      <div class="elements-list" id="slider1">
        <div class="element">
          <div class="element-text">
            Sylvester is a person of many talents. He's a great leader, coach and motivational speaker. Sylvester is always goal-oriented, very focused and patient person, which assures his recruiting services to be on a very high level. It was a great luck to have
            Sylvester as a team leader in our team. Sylvester is a person of many talents. He's a great leader, coach and motivational speaker. Sylvester is always goal-oriented, very focused and patient person, which assures his recruiting services to
            be on a very high level. It was a great luck to have Sylvester as a team leader in our team.
          </div>
        </div>
        <div class="element">
          <div class="element-text">
            2 Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsum exercitationem asperiores perspiciatis sapiente ducimus atque dicta labore dolores sequi ut.
          </div>
        </div>
        <div class="element">
          <div class="element-text">
            3 Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus id quas, quia consectetur repellat quis! Minus voluptatibus, dolores iure deserunt.
          </div>
        </div>
      </div>
    </div>

  </div>
  <div class="person-author">
    <div class="person-quotation">
      <span></span>
    </div>
    <div class="person-author-date">
      <a href="{{ url('#') }}" class="person-author-name-href">
        <div class="person-author-name">Jan Kowalski</div>
        <div class="person-author-workplace">Programista Java</div>
      </a>
    </div>
  </div>
</div>

答案 1 :(得分:1)

只需在类“ element-text”上设置一个“ height”即可

height:200px;

https://codepen.io/jarrodramsey/pen/JBaNQG

答案 2 :(得分:1)

如果要在溢出时滚动,则需要设置最大宽度高度,因此,如果您的文本大小大于该大小,它将滚动。

    overflow: scroll;
    max-width: 500px;
    max-height: 200px;