【问题标题】:Reduce opacity of a div in a horizontal scroll is it's partially visible降低水平滚动中 div 的不透明度是否部分可见
【发布时间】:2021-03-09 10:08:59
【问题描述】:

我正在使用 HTML 和 CSS 创建一个轮播,如下所示:

以下是代码:

//It checks whether a card is outside the viewport ):

const box = document.querySelector(".card");
const rect = box.getBoundingClientRect();

console.log(rect);

function isInViewport() {
  return (
    rect.top >= 0 &&
    rect.left >= 0 &&
    rect.bottom <=
    (window.innerHeight || document.documentElement.clientHeight) &&
    rect.right <= (window.innerWidth || document.documentElement.clientWidth)
  );
}

const result = isInViewport(box);
console.log(result);
document.addEventListener(
  "scroll",
  function() {
    console.log(result);
  }, {
    passive: true,
  }
);
.container {
  display: flex;
  overflow-x: scroll;
  height: 415px;
  width: 1455px;
  padding-left: 150px;
}

.card {
  box-sizing: border-box;
  height: 351px;
  width: 276px;
  border: 1px solid #dddbdb;
  background-color: #ffffff;
}

.card-img {
  height: 147px;
  width: 275px;
}

.card-head>p {
  height: 75px;
  width: 218px;
  color: #a72b2a;
  font-family: ".SF NS Display";
  font-size: 20px;
  letter-spacing: -0.33px;
  line-height: 25px;
}

.card-body {
  height: 50px;
  width: 233px;
  color: #535353;
  font-family: ".SF NS Display";
  font-size: 14px;
  letter-spacing: -0.23px;
  line-height: 18px;
}

.card-footer {
  display: flex;
  height: 28px;
  opacity: 0.7;
  color: #333333;
  font-family: ".SF NS Display";
  font-size: 14px;
  letter-spacing: 0;
  line-height: 28px;
}
<div class="container" id="container">
  <div class="card">
    <div class="card-img"></div>
    <div class="card-rectangle">
      <div class="card-head">
        <p>
          Infographic: Understanding the basics and opportunity of hydrogen
        </p>
      </div>
      <div class="card-body">
        <p>
          There is no silver-bullet sustainable energy solution; a net zero future will…
        </p>
      </div>
      <div class="card-footer">
        <div>
          <p>12/11/20</p>
        </div>
        <div>
          <p>|</p>
        </div>
        <div>
          <p>Kelly Jiang</p>
        </div>
      </div>
    </div>
  </div>

我想在 div 部分可见时降低它的不透明度。 (它可能在最左边或最右边)(例如 - 一个在最右边) 我该怎么做?

【问题讨论】:

  • 这是布尔玛,还是我错了?
  • 不,只是 CSS
  • .card 结构与 bulma 使用的 .card 结构非常相似 :) 如果该卡不在视野范围内,您是否要将整个不透明度应用到 .card,或者只是部分.card,使用梯度原理?
  • 如果部分看不到,我想申请整个.card

标签: javascript html jquery css web-frontend


【解决方案1】:

这是我最终想出的解决方案,几乎满足了我的需求:

添加了一个新的非活动类,该类在应用时会改变卡片的不透明度:

.card-inactive {
  opacity: 0.6;
}

每当卡片超出视口时,使用 JS 添加 .card-inactive。

const card = document.getElementsByClassName("card"); //grabs all the elements with .card class
const container = document.getElementsByClassName("second-container")[0]; //grabs the div with .second-container class

function isInViewport(el) {
  let result = [];
  for (var i = 0; i < el.length; i++) {
    const rect = el[i].getBoundingClientRect();
    // console.log(i, el.length);
    result.push(
      rect.left >= 0 &&
        rect.right <=
          (window.innerWidth || document.documentElement.clientWidth)
    ); // checks whether a div with the .card class is out of the viewport and stores the value in result[]
  }
  return result; // returns an array of boolean values for each element with .card class
}

container.addEventListener("scroll", function () {
  //listens to scroll event inside second-container
  const result = isInViewport(card);
  for (var i = 0; i < result.length; i++) {
    // Below code adds/removes .card-inactive class
    if (!result[i]) {
      card[i].classList.add("card-inactive");
    } else card[i].classList.remove("card-inactive");
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 2016-08-19
    • 2014-05-31
    • 2018-01-31
    • 2021-03-25
    • 2015-07-19
    相关资源
    最近更新 更多