代码分享-模仿高德官网效果做的

代码分享-模仿高德官网效果做的

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>高德地图线路导航</title>

    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

    <!-- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.2/animate.min.css"> -->

    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=598752f778bdf24aee04bbf081d10a1d&plugin=AMap.Autocomplete,AMap.Scale,AMap.ToolBar,AMap.ControlBar,AMap.MapType,AMap.Driving,AMap.Transfer,AMap.Riding,AMap.PlaceSearch"></script> 

    <style>

        #container {

            width: 100%;

            height: 100%;

            position: absolute;

            right: 0;

            top: 0;

        }

        #searchBox {

            width: 300px;

            position: absolute;

            left: 20px;

            top: 20px;

            background-color: #3d93fd;

            border-radius: 5px;

            text-align: center;

        }

        #searchResult {

            width: 300px;

            max-height: 380px;

            background-color: white;

            border: none;

            position: relative;

            overflow-x: hidden;

            animation:slideInDown 1s;

            /* display: none; */

            border-bottom: 1px solid rgb(214, 214, 214);

        }

        @keyframes slideInDown {

            from {max-height: 0;}

            to {max-height: 380px;}

        }        

        #searchIcon {

            margin: 15px;

            color: white; 

            position: relative;

        }

        i {opacity: 0.5;

            height: 20px;

            margin: 10px;

            cursor: pointer;

            margin: 0 15px;

            padding: 0 5px;

        }

        @keyframes slideInUp {

            from {

                transform: translate3d(0, 10%, 0);

                opacity: 0;

                visibility: visible;

            }

            to {

                transform: translate3d(0, 0, 0);

                opacity: 1;

            }

        }

        @keyframes slideOutDown {

            from {

                transform: translate3d(0, 0, 0);

                opacity: 1;

            }

            to {

                visibility: hidden;

                transform: translate3d(0, 10%, 0);

                opacity: 0;

            }

        }

        .tip {

            position: absolute;

            float: left;

            color: white;

            top: 45px;

            left: 65px;

            z-index: 10;

            font-size: 10px;

            background-color: rgba(0, 0, 0, 0.7);

            padding: 7px 10px;

            border-radius: 2px;

            display: none;

            animation:slideInUp 150ms linear;

        }

        #tip2 {

            left: 125px;

        }

        #tip3 {

            left: 190px;

        }

        .tip .j {

            position: absolute;

            left: 50%;

            top: -10px;

            margin-left: -5px;

            /* display: block; */

        }

        .tip .j::before,.tip .j::after {

            content: "";

            /* display: block; */

            width: 0;

            height: 0;

            border-width: 5px;

            border-style: solid;

            position: absolute;

            left: 0;

            top: 0;

        }

        .tip .j::before {

            border-color: transparent transparent rgba(0, 0, 0, 0.4) transparent;

        }

        .tip .j::after {

            border-color: transparent transparent rgba(0, 0, 0, 0.4) transparent;

            top: 1px;

        }

        .searchIpt {

            position: relative;

            height: 30px;

            line-height: 30px;

            width: 250px;

            font-size: 14px;

            color: #acd7ff;

            background-color: #3587eb;

            border-radius: 2px;

            margin-bottom: 10px;

            margin-left: 25px;

            text-align: left;

        }

        label {

            padding: 0 0 0 10px;

        }

        input {

            width: 75%;

            font-size: 14px;

            line-height: 27px;

            color: #e1f1ff;

            border: none;

            text-indent: 5px;

            background: transparent;

            text-indent: 5px;

            outline: 0;

            caret-color:white;

        }


        input::-webkit-input-placeholder,

        textarea::-webkit-input-placeholder {

        /* WebKit browsers */

            color: #acd7ff;

        }

        input:-moz-placeholder,

        textarea:-moz-placeholder {

        /* Mozilla Firefox 4 to 18 */

            color: #acd7ff;

        }

        input::-moz-placeholder,

        textarea::-moz-placeholder {

        /* Mozilla Firefox 19+ */

            color: #acd7ff;

        }

        input:-ms-input-placeholder,

        textarea:-ms-input-placeholder {

        /* Internet Explorer 10+ */

            color: #acd7ff;

        }


        #searchBtn {

            background-color: #559ffb;

            color: white;

            border: none;

            height: 30px;

            margin: 10px 25px 20px 15px;

            padding: 0 20px;

            border-radius: 2px;

            box-shadow: 0 1px 1px 0 rgba(0,0,0,.21);

            cursor: pointer;

            float: right;

        }

    </style>

</head>

<body>

    <div id="container"></div>

    <div id="searchBox">

        <div id="searchIcon">

            <i class="fa fa-car" id="car"></i>

            <i class="fa fa-bus" id="bus"></i>

            <i class="fa fa-bicycle" id="bicycle"></i>

        </div>

        <div>

            <div class="tip" id="tip1"><span class="j"></span>驾车</div>

            <div class="tip" id="tip2"><span class="j"></span>公交</div>

            <div class="tip" id="tip3"><span class="j"></span>骑行</div>

        </div>

        <div class="searchIpt">

            <label>起</label>

            <input id="start" type="text" placeholder="请输入起点">

        </div>

        <div class="searchIpt">

            <label>终</label>

            <input id="end" type="text" placeholder="请输入终点">

        </div>

        <div id="footer">

            <button id="searchBtn" class="slideInDown">开车去</button>

        </div>

        <div id="searchResult"></div>

    </div>

    <script>

        car.style.opacity = 1;

        //初始化地图

        var map = new AMap.Map('container',{

            zoom:10,

            center: [114.305215,30.592935],

        })

        //添加输入提示并将结果显示在搜索结果列表中

        var startNode = new AMap.Autocomplete({

            input: 'start',

        })

        var endNode = new AMap.Autocomplete({

            input: 'end',

        })

        var placeSearch = new AMap.PlaceSearch({

            map: map,

            pageSize: 5,

            panel: 'searchResult'

        })

        AMap.event.addListener(startNode,'select',function(e){

            map.clearMap();

            map.setCenter(e.poi.location);

            map.setZoom(12);

            placeSearch.search(e.poi.name);

        })

        AMap.event.addListener(endNode,'select',function(e){

            map.clearMap();

            map.setCenter(e.poi.location);

            map.setZoom(12);

            placeSearch.search(e.poi.name);

        })

        //添加图标交互动画及点击事件

        var iconArr = document.getElementsByClassName('fa');

        var travelMode = ['开车去','坐公交','骑车去']

        var tips = document.getElementsByClassName('tip');

        function iconReset(){

            for (let i = 0; i < iconArr.length; i++) {

                iconArr[i].style.opacity = 0.5;

            }

        }

        for (let i = 0; i < iconArr.length; i++) {

            const icon = iconArr[i];

            const tip = tips[i];

            icon.onclick = ()=>{

                iconReset();

                icon.style.opacity = 1;

                searchBtn.innerHTML = travelMode[i];

                if (start.value != '' & end.value != '') {

                    Go();

                }

            };

            icon.onmouseover = ()=>{

                if (icon.style.opacity !=1) {

                    icon.style.opacity = 0.75;

                }

                tip.style.display = 'block';

                tip.style.animation = 'slideInUp 150ms linear';

            };

            icon.onmouseout = ()=>{

                if (icon.style.opacity ==0.75) {

                    icon.style.opacity = 0.5;

                }

                tip.style.animation = 'slideOutDown 150ms linear';

                setTimeout(() => {

                    tip.style.display = 'none';

                }, 150);

            };

        }

        //按钮点击事件

        searchBtn.onclick = function () {

            Go();

        }

        //封装出行方式函数byCar()/byBus()/byBike()

        function byCar(){

            new AMap.Driving({

                map: map,

                panel: 'searchResult'

            }).search([

                {keyword:start.value},

                {keyword:end.value},

            ],(status,data)=>{});

        }

        function byBus(){

            map.getCity(function (data) {

                var city = data.city;

                if (data.city == '') {

                    city = data.province;

                }

                new AMap.Transfer({

                    map: map,

                    panel: 'searchResult'

                }).search([

                    {keyword:start.value,city:city},

                    {keyword:end.value,city:city},

                ],(status,data)=>{});

            })

        }

        function byBike() {

            new AMap.Riding({

                map: map,

                panel: 'searchResult'

            }).search([

                {keyword:start.value},

                {keyword:end.value},

            ],(status,data)=>{});

        }

        //封装搜索线路函数Go()

        function Go(){

            map.clearMap();

            searchResult.innerHTML = '';

            searchResult.style.display = 'none';

            if (searchBtn.innerHTML == '开车去') {

                byCar();

            }

            else if (searchBtn.innerHTML == '坐公交') {

                byBus();

            }

            else if (searchBtn.innerHTML == '骑车去') {

                byBike();

            }

            setTimeout(() => {

                searchResult.style.display = 'block';

            }, 1000);

        }

    </script>

</body>

</html>


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕夭夭 2019-12-27 10:34:13

同学你好,代码实现的很不错,也感谢同学的分享,相信能够帮助到其他小伙伴。

继续加油,祝学习愉快!

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
3.WebAPP开发与小程序
  • 参与学习           人
  • 提交作业       622    份
  • 解答问题       6815    个

微信带火了小程序,也让前端工程师有了更多的展现机会,本阶段带你从移动基础知识的学习到webAPP开发,及小程序开发,让你PC端与移动端两端通吃。

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师