代码之家  ›  专栏  ›  技术社区  ›  Gabriel

点击触发时Div不考虑宽度和高度

  •  1
  • Gabriel  · 技术社区  · 2 年前

    当用户单击图像时,下面的片段应该用Javascript函数加载的窗口替换静态图像。问题是加载的Javascript块不考虑宽度和高度,如果 div 元素,它被挤压到左边。

    我做错了什么?

    <div style="display: flex; justify-content: space-between;">
     <div style="text-align: center;">
    
    
    <!-- Your image -->
    <img id="myImage" src="https://raw.githubusercontent.com/ucc23/test/main/berkeley29.webp" alt="Clickable Image" style="width:355px;height:250px; cursor: pointer;">
    
    
    <!-- Div to contain Aladin Lite viewer -->
    <div id="aladin-lite-div" style="width:355;height:250"></div>
    
    <!-- Aladin Lite script (will be loaded after the image is clicked) -->
    <script type="text/javascript">
    // Function to load Aladin Lite after image click and hide the image
    function loadAladinLiteAndHideImage() {
        // Dynamically load the Aladin Lite script
        let aladinScript = document.createElement('script');
        aladinScript.src = "https://aladin.cds.unistra.fr/AladinLite/api/v3/latest/aladin.js";
        aladinScript.charset = "utf-8";
        aladinScript.onload = function () {
            A.init.then(() => {
                let aladin = A.aladin('#aladin-lite-div', {survey:"P/DSS2/color", fov:0.1, target: "103.269 16.929"});
                // Remove the image
                document.getElementById('myImage').remove();
                // Hide the image
                //document.getElementById('myImage').style.visibility = "hidden";
                          // Show the Aladin Lite viewer
                document.getElementById('aladin-lite-div').style.display = 'block';
    });
         };
        document.head.appendChild(aladinScript);
    }
    // Event listener for image click
    document.getElementById('myImage').addEventListener('click', loadAladinLiteAndHideImage);
    </script>
    
    </div>
    <!-- Left block -->
    
    <table style="text-align: center; width:355px;height:250px;">
      <!-- Row 1 (title) -->
      <tr>
        <td colspan="5"><h3>Table title</h3></td>
      </tr>
      <!-- Row 2 -->
      <tr>
        <th><a>C1</a></th>
        <th><a>C2</a></th>
        <th><a>C3</a></th>
        <th><a>C4</a></th>
        <th><a>C5</a></th>
      </tr>
      <!-- Row 3 -->
      <tr>
        <td>1.0</td>
        <td>0.52</td>
        <td>9.7</td>
        <td>534</td>
        <td>3.0</td>
      </tr>
    </table>
    </div>
    1 回复  |  直到 2 年前
        1
  •  1
  •   CodeApacalypse    2 年前

    Aladin-Lite查看器不尊重其宽度和高度的问题 div 元素是由于缺少 px 单位 style 的属性 div 。在代码中,您已将宽度和高度指定为 width:355;height:250 ,但您应该包括 px 正确定义尺寸的单位。

    若要解决此问题,请更新 风格 的属性 div 元素如下:

    <div id="aladin-lite-div" style="width:355px;height:250px"></div>
    

    通过添加 px 单位,Aladin-Lite查看器现在应该尊重其宽度和高度 div 元素,并且内容不应再向左挤压。