求指教, html 的切换功能实现,以及为何会导致同时加载的 div 之一填不满页面

2020-06-19 14:37:23 +08:00
 6167

前端小白请教,两个 div 同时加载,其中一个初始隐藏 display=none,同时做俩个按钮切换两个 div,最终展示的效果却使初始隐藏的子 div 缩小了很多,解决方案已有,求为何会这样?

<html> <head> <meta charset="utf-8"> <title>数据可视化</title> <script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script> </head> <body>
<!-- Dom1:bar_chart -->
<div style="width:100%;height:400px;">
    <div id="map" style="width: 100%;height:100%;"></div>
    <div id="bar" style="width: 100%;height:100%;display: none;"></div>
    <button class="shift-button" id="map-button" type="button" onclick="useMap()" style="background-color: #FF1815">
        条形图
    </button>
    <!--使用柱状图加载-->
    <button class="shift-button" id="bar-button" type="button" onclick="useBar()" style="background-color: grey;margin-left: 0.2rem">
        折线图
    </button>
</div>

<script type="text/javascript">
    // 基于准备好的 dom,初始化 echarts 实例
    // var echarts = require('echarts');
    var myChart = echarts.init(document.getElementById('map'));

    // 空图表
    var option = {
        title: {
            text: '同时间各地区业务数据'
        },
        tooltip: {},
        legend: {
            data:['业务量']
        },
        xAxis: {
            data: [1,2,3,4,5,6,7]
        },
        yAxis: {},
        series: [{
            name: '业务量',
            type: 'bar',
            data: [1,2,3,4,5,6,7]
        }]
    };

    myChart.setOption(option);

    var lineChart = echarts.init(document.getElementById('bar'));
    
        // 指定图表的配置项和数据
        var lineOption = {
            title: {
                text: '地区业务量数据'
            },
            tooltip: {},
            legend: {
                data:['销量']
            },
            xAxis: {
                data: [1,2,3,4,5,6]
            },
            yAxis: {},
            series: [{
                name: '业务量',
                type: 'line',
                data: [5, 20, 36, 10, 10, 20]
            }]
        };

        // 使用刚指定的配置项和数据显示图表。
        lineChart.setOption(lineOption);

    // 基于准备好的 dom,初始化 echarts 实例
    // var echarts = require('echarts');
    
</script>

<script>
    //切换地图
    function useMap(){
        document.getElementById("map").style.display="";
        document.getElementById("map-button").style.backgroundColor="#FF1815";
        document.getElementById("bar").style.display="none";
        document.getElementById("bar-button").style.backgroundColor="grey";


        //document.getElementById("line").style.display="none";
    }

    //切换柱状图
    function useBar(){
        document.getElementById("map").style.display="none";
        document.getElementById("map-button").style.backgroundColor="grey";
        document.getElementById("bar").style.display="";
        document.getElementById("bar-button").style.backgroundColor="#FF1815";
        //document.getElementById("line").style.display="none";
        
    }
</script>
</body> </html>
2028 次点击
所在节点    HTML
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/683038

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX