javascript - 是否有可能获得放置在背景图像顶部的 div 后面的颜色?

标签 javascript jquery html css

如果您有一个覆盖整个 HTML 页面主体的背景图像,并且您将主体分成几个 div(像网格一样思考),是否可以分辨出 HTML 页面中心点后面的颜色是什么?分区?

The blue grid is divs. The Dots represent the center point of the div and what color I want to find out about

在此图像中,蓝色网格代表“divs”。点代表 div 的中心点和我想了解的颜色。

最佳答案

我在 2 个答案(thisthis)的帮助下找到了解决方案。

我创建了 2 个 div:

1 - 使用封面图片。

2 - 网格容器。

image-div 位于 grid-container 的前面。 当您点击图片时,它会保存点击点的坐标,并将 image-div 的 z-index 更改为在后面。

然后使用相同的坐标模拟单击文档(网格容器现在在前面),以获取 css 属性。

最后再次切换显示在前面的image-div的z-index。

请在全窗口运行以下代码或在提供的fiddle中查看.

var posX, posY;
    $("#cover").click(function(e){
    
    //get cursor coordinates
    posX = e.pageX;
    posY = e.pageY;
    
    //push cover image to the back of the view
    $(this).css("z-index",-1);
    
    //simulate click with same coordinates on grid-container
    document.elementFromPoint(posX, posY).click();
    
    //switch cover image back to the front of the view
    $(this).css("z-index",2);
    });
    
    //get bgColor and use it
    $(".grid-item").click(function(){
    var bgColor = $(this).css("background-color");
    //use bgColor as you like
    $("#result").css("background-color",bgColor);
    })
#cover, .grid-container {
    width: 100%;
    height: 80%;
    position: absolute;
    top: 0;
    }
    #cover {
    z-index: 2;
    background-image: url('https://www.gstatic.com/webp/gallery/4.sm.jpg');
    background-size: 100% 100%;
    }

    .grid-container {
      display: grid;
      grid-template-columns: auto auto auto;
    }

    .grid-item {
      padding: 20px;
      font-size: 40px;
      text-align: center;
    }
    #grid-item-1 { background-color: #00061e; }
    #grid-item-2 { background-color: #394266; }
    #grid-item-3 { background-color: #61a7d3; }
    #grid-item-4 { background-color: #db2f1c; }
    #grid-item-5 { background-color: #8b8f91; }
    #grid-item-6 { background-color: #ce171d; }
    #grid-item-7 { background-color: #92ed5a; }
    #grid-item-8 { background-color: #db25db; }
    #grid-item-9 { background-color: #fcf00b; }
    span { 
    position:absolute;
    top:85%;
    }
    #result {
     width:50px;
     height:50px;
     left:200px;
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div>

    <div id="cover"></div>

     <div class="grid-container">
      <div class="grid-item" id="grid-item-1">1</div>
      <div class="grid-item" id="grid-item-2">2</div>
      <div class="grid-item" id="grid-item-3">3</div>
      <div class="grid-item" id="grid-item-4">4</div>
      <div class="grid-item" id="grid-item-5">5</div>
      <div class="grid-item" id="grid-item-6">6</div>
      <div class="grid-item" id="grid-item-7">7</div>
      <div class="grid-item" id="grid-item-8">8</div>
      <div class="grid-item" id="grid-item-9">9</div>
    </div> 

    </div>

    <span>background-color result:</span>
    <span id="result"></span>

关于javascript - 是否有可能获得放置在背景图像顶部的 div 后面的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54848081/

相关文章:

javascript - 我可以检测文本中的非日文汉字吗

Javascript:if 语法中的附加零是什么?

html - Flying Saucer 对细胞图像的处理是一个错误吗?

javascript - jQuery 重定向到另一个文件

javascript - 当我的键是数字(1,2,3 ....)时,如何访问 JSON 中的数据元素

javascript - react : render list Object directly from function inside <ul></ul> with an id?

javascript - 如何使用 Javascript 从 APEX_ITEM 设置页面元素的值

javascript - 找出 <td> 元素内 <span> 元素的数量

jquery - 如何旋转 HTML5 Canvas 的背景图像并使用 slider 放大和缩小?

javascript - 如何依次淡出和淡入div