【问题标题】:CSS z-index property and javascript background mouse interactionCSS z-index 属性和 javascript 背景鼠标交互
【发布时间】:2020-11-14 02:30:08
【问题描述】:

我正在尝试添加一个径向渐变作为我的博文卡片的背景。 当 博客明信片悬停。这里我的小代码示例关于 效果(https://codepen.io/D_s/pen/OJNpNBV)。

问题在于它不能按预期工作,因为卡片的其他 div 元素(标题、图像、标签、按钮)正在干扰悬停。我怎么能解决这个问题? 以下是博文卡片的代码:

let backgroundgradient = document.querySelector('.card');
backgroundgradient.onmousemove = function(e) {
let rect = e.target.getBoundingClientRect();
let x = e.clientX - rect.left;
let y = e.clientY - rect.top;
backgroundgradient.style.setProperty('--x', x + 'px');
backgroundgradient.style.setProperty('--y', y + 'px');
}

感谢您的帮助!

【问题讨论】:

    标签: javascript css flexbox


    【解决方案1】:

    考虑使用.card 元素本身的.offsetLeft/.offsetRight,而不是使用e.target.getBoundingClientRect() 和.left/.right,它们会随着子元素的悬停而改变。

    let backgroundgradient = document.querySelector('.card');
    backgroundgradient.onmousemove = function(e) {
      let x = e.clientX - backgroundgradient.offsetLeft;
      let y = e.clientY - (backgroundgradient.offsetTop - window.pageYOffset);
      backgroundgradient.style.setProperty('--x', x + 'px');
      backgroundgradient.style.setProperty('--y', y + 'px');
    }
    .card,
    .imagebox {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
    }
    
    .imagebox {
      flex: 3 1 30ch;
      height: calc(282px + 5vw);
      overflow: hidden;
    }
    
    .imagebox img {
      max-width: 100%;
      min-width: 100%;
      min-height: 100%;
      max-height: 100%;
      width: auto;
      height: auto;
      object-fit: cover;
      object-position: 50% 50%;
      padding: 15px;
      border-radius: 30px;
      z-index: 1;
    }
    
    .card {
      background: #bdbdbd;
      box-sizing: border-box;
      border: 2px solid #f93668;
      overflow: hidden;
      position: relative;
      border-radius: 20px;
      overflow: hidden;
    }
    
    .card:before {
      --size: 0;
      content: '';
      position: absolute;
      left: var(--x);
      top: var(--y);
      width: var(--size);
      height: var(--size);
      background: radial-gradient(circle closest-side, #f93668, transparent);
      transform: translate(-50%, -50%);
      transition: width 0.8s ease, height 0.4s ease;
    }
    
    .card:hover:before {
      --size: 90rem;
    }
    
    .card-content {
      padding: 16px 32px;
      flex: 10 1 40ch;
      z-index: 2;
    }
    
    .card-tags {
      margin: 0 -8px;
    }
    
    .card-tag {
      display: inline-block;
      margin: 8px;
      font-size: 0.875em;
      text-transform: uppercase;
      font-weight: 600;
      letter-spacing: 0.02em;
      color: var(--primary);
    }
    
    .card-title {
      margin: 0;
      font-size: clamp(2.4em, 1.1vw, 1.1em);
      font-family: "Roboto Slab", Helvetica, Arial, sans-serif;
    }
    
    .card-title a {
      text-decoration: none;
      color: inherit;
    }
    
    .card-metadata {
      margin: 0;
    }
    
    .card-save {
      display: flex;
      align-items: center;
      padding: 6px 14px 6px 12px;
      border-radius: 4px;
      border: 2px solid currentColor;
      color: var(--primary);
      background: none;
      cursor: pointer;
      font-weight: bold;
    }
    
    .card-save svg {
      margin-right: 6px;
    }
    
    
    /* Body Layout */
    
    * {
      box-sizing: border-box;
    }
    
    body {
      --primary: rgb(249, 54, 104);
      --grey: #454545;
      --lightgrey: #666;
      color: var(--grey);
      line-height: 1.55;
      display: flex;
      justify-content: center;
      flex-wrap: wrap;
      font-family: "Roboto", Helvetica, Arial, sans-serif;
    }
    
    .container {
      width: clamp(20px, 90%, 90%);
      padding: 24px;
    }
    <div class="container">
      <article class="card">
        <div class="imagebox">
          <img src="https://d2w9rnfcy7mm78.cloudfront.net/2548871/original_eb4afb32258fcb72de6dddd99cbc4171.jpg?1534159969?bc=1" width="1500" height="1368" alt="">
        </div>
        <div class="card-content">
          <p class="card-tags">
            <span class="card-tag">Gluten Free</span>
            <span class="card-tag">Main dish</span>
          </p>
          <h1 class="card-title"><a href="#">Title for a post to come</a></h1>
    
          <p class="card-desc">by Slutty Urbanism</p>
    
          <button class="card-save" type="button">
                    Read more
                </button>
    
        </div>
      </article>
    
    </div>

    具有多个卡片元素的示例...

    let cards = document.querySelectorAll('.card');
    
    for (let card of cards) {
      card.onmousemove = function(e) {
        let x = e.clientX - card.offsetLeft;
        let y = e.clientY - (card.offsetTop - window.pageYOffset);
        card.style.setProperty('--x', x + 'px');
        card.style.setProperty('--y', y + 'px');
      }
    }
    .card,
    .imagebox {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
    }
    
    .imagebox {
      flex: 3 1 30ch;
      height: calc(282px + 5vw);
      overflow: hidden;
    }
    .imagebox img {
      max-width: 100%;
      min-width: 100%;
      min-height: 100%;
      max-height: 100%;
      width: auto;
      height: auto;
      object-fit: cover;
      object-position: 50% 50%;
      padding: 15px;
      border-radius: 30px;
      z-index: 1;
    }
    
    .card {
      background: #bdbdbd;
      box-sizing: border-box;
      border: 2px solid #f93668;
      overflow: hidden;
      position: relative;
      border-radius: 20px;
      overflow: hidden;
    }
    
    .card:before {
      --size: 0;
      content: '';
      position: absolute;
      left: var(--x);
      top: var(--y);
      width: var(--size);
      height: var(--size);
      background: radial-gradient(circle closest-side, #f93668, transparent);
      transform: translate(-50%, -50%);
      transition: width 0.8s ease, height 0.4s ease;
    }
    
    .card:hover:before {
      --size: 90rem;
    }
    
    .card-content {
      padding: 16px 32px;
      flex: 10 1 40ch;
      z-index: 2;
    }
    .card-tags {
      margin: 0 -8px;
    }
    .card-tag {
      display: inline-block;
      margin: 8px;
      font-size: 0.875em;
      text-transform: uppercase;
      font-weight: 600;
      letter-spacing: 0.02em;
      color: var(--primary);
    }
    .card-title {
      margin: 0;
      font-size: clamp(2.4em, 1.1vw, 1.1em);
      font-family: "Roboto Slab", Helvetica, Arial, sans-serif;
    }
    .card-title a {
      text-decoration: none;
      color: inherit;
    }
    .card-metadata {
      margin: 0;
    }
    .card-save {
      display: flex;
      align-items: center;
      padding: 6px 14px 6px 12px;
      border-radius: 4px;
      border: 2px solid currentColor;
      color: var(--primary);
      background: none;
      cursor: pointer;
      font-weight: bold;
    }
    .card-save svg {
      margin-right: 6px;
    }
    
    /* Body Layout */
    * {
      box-sizing: border-box;
    }
    
    body {
      --primary: rgb(249, 54, 104);
      --grey: #454545;
      --lightgrey: #666;
      color: var(--grey);
      line-height: 1.55;
      display: flex;
      justify-content: center;
      flex-wrap: wrap;
      font-family: "Roboto", Helvetica, Arial, sans-serif;
    }
    
    .container {
      width: clamp(20px, 90%, 90%);
      padding: 24px;
    }
    <div class="container">
      <article class="card">
        <div class="imagebox">
          <img src="https://d2w9rnfcy7mm78.cloudfront.net/2548871/original_eb4afb32258fcb72de6dddd99cbc4171.jpg?1534159969?bc=1" width="1500" height="1368" alt="">
        </div>
        <div class="card-content">
          <p class="card-tags">
            <span class="card-tag">Gluten Free</span>
            <span class="card-tag">Main dish</span>
          </p>
          <h1 class="card-title"><a href="#">Title for a post to come</a></h1>
    
          <p class="card-desc">by Slutty Urbanism</p>
    
          <button class="card-save" type="button">
            Read more
          </button>
    
        </div>
      </article>
    
      <article class="card">
        <div class="imagebox">
          <img src="https://d2w9rnfcy7mm78.cloudfront.net/2548871/original_eb4afb32258fcb72de6dddd99cbc4171.jpg?1534159969?bc=1" width="1500" height="1368" alt="">
        </div>
        <div class="card-content">
          <p class="card-tags">
            <span class="card-tag">Gluten Free</span>
            <span class="card-tag">Main dish</span>
          </p>
          <h1 class="card-title"><a href="#">Title for a post to come</a></h1>
    
          <p class="card-desc">by Slutty Urbanism</p>
    
          <button class="card-save" type="button">
            Read more
          </button>
    
        </div>
      </article>
    
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      • 1970-01-01
      相关资源
      最近更新 更多