使用 box-shadow 属性创建两个聚光灯

问题描述 投票:0回答:1

我正在尝试创建一个同时具有两个聚光灯的指南。当我尝试添加另一个聚光灯时,两个聚光灯区域看起来都是阴影。如何避免一个聚光区域的阴影与其他聚光区域重叠?框阴影是为了突出显示所需区域,同时使页面的其他部分看起来像是在其上方有覆盖层。

这是可以尝试的小提琴链接。 https://jsfiddle.net/ad1xzbwy/

只有一盏聚光灯 这是只有一盏聚光灯时的样子。 两个聚光灯 这是有两个聚光灯时的样子。

这是简单示例的代码。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Centered Triangle Text</title>
<style>
  body, html {
    height: 100%;
    margin: 0;
    position: relative;
    scroll-behavior: smooth;
  }
  .article {
    margin-top: 200px;
    height: 500px;
    background-color: #f0f0f0;
  }
  
  .vertical-timeline {
    position: fixed;
    top: 50px;
    left: 50px;
    width: 5px;
    background-color: #CC0E19;
    z-index: 1;
    height: 80px;
    z-index: 100;
  }
  .label {
    position: absolute;
    left: 5px;
    z-index: 2;
    cursor: pointer;
  }
  .label-pentagon {
    background-color: #CC0E19;
    width: 105px;
    height: 30px;
    clip-path: polygon(0 0, calc(100% - 30px) 0, 100% 50%, calc(100% - 30px) 100%, 0 100%);
    color: white;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    padding-left:5px;
    font-size: 0.9rem;
  }
.label-triangle {
    position: absolute;
    top: 20px;
    left: 5px;
    width: 0;
    height: 0;
    border-top: 10px solid transparent;
    border-left: 15px solid #CC0E19;
    border-bottom: 10px solid transparent;
}
.triangle-text {
    position: absolute;
    left: 30px;
    top: 20px;
    color: #CC0E19;
    white-space: nowrap;
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    height: 20px;
}

  .spotlight-active .overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    /*background: rgba(211, 211, 211, 0.7);*/
    z-index: 50; /* Above the content but below the labels */
    display: block; /* Show the overlay */
  }
  .spotlight-active .spotlight.desktop {
    position: fixed;
    top: 20px;
    left: 35px;
    width: 130px;
    height: 130px;
    border-radius: 50%;
    z-index: 51; /* Above the overlay */
    box-shadow: 0 0 0 10000px rgba(0, 0, 0, 0.7); /* Large shadow with a "cut-out" effect */
    display: block; /* Show the spotlight */
    background-clip: content-box; /* Ensures the inner background is clipped to the content box */
  }
  
  .spotlight-active .spotlight-2.desktop {
    position: fixed;
    top: 20px;
    left: 350px;
    width: 130px;
    height: 130px;
    border-radius: 50%;
    z-index: 51; /* Above the overlay */
    box-shadow: 0 0 0 10000px rgba(0, 0, 0, 0.7); /* Large shadow with a "cut-out" effect */
    display: block; /* Show the spotlight */
    background-clip: content-box; /* Ensures the inner background is clipped to the content box */
  }

.message-zone {
  position: fixed;
  top: 150px;
  left: 320px;
  transform: translateX(-50%);
  z-index: 52;
  text-align: center;
  max-width: 220px;
}
.message-box {
  background-color: white;
  color: black;
  padding: 10px;
  border-radius: 5px;
  z-index: 52;
  text-align: center;
  max-width: 220px;
}
.ok-button {
  display: block;
  margin: 10px auto;
  background-color: #CC0E19;
  color: white;
  border: none;
  padding: 5px 20px;
  border-radius: 5px;
  cursor: pointer;
  float: right;
}  
.arrow-guide {
  position: fixed;
  top: 130px;
  left: 140px;
  z-index: 102;
}

.spotlight-active .spotlight.mobile {
    position: fixed;
    top: 100px;
    left: -20px;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    z-index: 51;
    box-shadow: 0 0 0 10000px rgba(0, 0, 0, 0.7);
    display: block;
    background-clip: content-box;
}
.message-zone.mobile {
  position: fixed;
  top: 150px;
  left: 320px;
  transform: translateX(-50%);
  z-index: 52;
  text-align: center;
  max-width: 220px;
}
.arrow-guide.mobile {
  position: fixed;
  top: 130px;
  left: 140px;
  z-index: 102;
}


  .switch-article-overlay {
    display: flex;
    position: fixed;
    left: 0;
    top: 110px;
    z-index:99999;
  }
  .switch-article-overlay button.overlay-icon {
    background-color: #CC0E19;
    color: #fff;
    font-size: 3em;
    height: 30px;
    max-width: none;
    width: 0.3em;
    border-top-right-radius: 0.1em;
    border-bottom-right-radius: 0.1em;
    border: 1px solid #636363;
    border-left-color: transparent;
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;    
  }
  button.overlay-icon {
    background: 0 0;
    border: none;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
    cursor: pointer;
    visibility: visible;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .switch-article-overlay button.overlay-icon svg {
    fill: #fff;
  }
  
</style>
</head>

<body>

<div class="overlay"></div>
<div class="spotlight desktop"></div>
<div class="spotlight-2 desktop"></div>

<div class="vertical-timeline mobile-hidden">
  <div class="label" style="top:0px;" onclick="scrollToArticle('article1')">
    <div class="label-pentagon">Main News</div>
  </div>
  <div class="label" style="top:30px;" onclick="scrollToArticle('article2')">
    <div class="label-triangle"></div>
    <div class="triangle-text">Second News</div>
  </div>
</div>

<div class="message-zone">
    <div class="message-box">
      <div class="message-content">Click to read the next news</div>
    </div>
  <button class="ok-button" onclick="hideSpotlight()">OK</button>
</div>

<img class="arrow-guide" src="arrow-left-white.png"/>


<div class="switch-article-overlay">
  <button class="overlay-icon" title="article switching">  
    <!--
    right arrow
    <svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24">
      <path d="M0 0h24v24H0V0z" fill="none"></path>
      <path d="M15.41 16.59L10.83 12l4.58-4.59L14 6l-6 6 6 6 1.41-1.41z"></path>
    </svg>
    -->    
    <svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24">
    <path d="M0 0h24v24H0V0z" fill="none"></path>
    <path d="M8.59 7.41L13.17 12l-4.58 4.59L10 18l6-6-6-6-1.41 1.41z"></path>
    </svg>
  </button>
</div>


<div id="article1" class="article">
  <h2>Article 1</h2>
</div>

<div id="article2" class="article">
  <h2>Article 2</h2>
</div>

<script>

function scrollToArticle(articleId) {
  const articleElement = document.getElementById(articleId);
  if (articleElement) {
    window.scrollTo({
      top: articleElement.offsetTop,
      behavior: 'smooth'
    });
  }
}

// Check if it's the user's first visit and apply the spotlight effect if it is
window.addEventListener('load', function() {
  if (!localStorage.getItem('hasVisited')) {
    document.body.classList.add('spotlight-active');
    localStorage.setItem('hasVisited', 'true');
  }
  
  //for testing use
  document.body.classList.add('spotlight-active');
  
});

// Optional: Close the spotlight effect on click or after a certain time
document.querySelector('.overlay').addEventListener('click', function() {
  document.body.classList.remove('spotlight-active');
  hideSpotlight();
});

function hideSpotlight() {
  // Hide overlay,spotlight, message box and arrow guide
  var overlay = document.querySelector('.overlay');
  var spotlight = document.querySelector('.spotlight');
  var spotlight2 = document.querySelector('.spotlight-2');
  var messageZone = document.querySelector('.message-zone');
  var arrowGuide = document.querySelector('.arrow-guide');
  overlay.style.display = 'none';
  spotlight.style.display = 'none';
  spotlight2.style.display = 'none';
  messageZone.style.display = 'none';
  arrowGuide.style.display = 'none';
}

$(document).ready(function(){
  showArticleSwitchWidget();
});

function showArticleSwitchWidget() {
    //$(".switch-article-overlay .overlay-label").addClass('hidden');
    $(".switch-article-overlay .overlay-icon svg path").attr( "d", "M8.59 16.59L13.17 12 8.59 7.41 10 6l6 6-6 6-1.41-1.41z");
}
function hideArticleSwitchWidget() {
    $(".switch-article-overlay .overlay-icon svg path").attr("d","M15.41 16.59L10.83 12l4.58-4.59L14 6l-6 6 6 6 1.41-1.41z");
}

</script>

</body>
</html>

曾尝试寻找答案,但没有找到。尽量保持 box-shadow 属性。

javascript html css angular
1个回答
0
投票

并没有真正使用 box-shadow,但我相信使用以下技术您可以得到几乎相同的结果:

.page  {
    display: grid;
    gap: 6px;
}

.page div {
  background-color: lightblue;
}

.spots {
  width: 100vw;
  height: 100vh;
  background-color: silver;
  z-index: 10;
  position: absolute;
  top: 0px;
  mix-blend-mode: darken;
}
.spots div {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  background-color: white;
  position: fixed;
}

#spot1 {
  left: 60px;
  top: 20px;
}


#spot2 {
  left: 150px;
  top: 50px;
}
<div class="page">
<div>line 1</div>
<div>line 1</div>
<div>line 1</div>
<div>line 1</div>
<div>line 1</div>
<div>line 1</div>
<div>line 1</div>
</div>
<div class="spots">
<div id="spot1"></div>
<div id="spot2"></div>
</div>

© www.soinside.com 2019 - 2024. All rights reserved.