耀炎食品-1.index

这篇博客详细介绍了如何使用HTML和JavaScript实现网页顶部导航栏的下拉效果,以及按钮点击后页面内容的切换。通过`mouseover`和`mouseleave`事件处理下拉菜单的显示与隐藏,并利用`onclick`事件改变按钮样式以及实现图片的切换功能。同时,通过修改`iframe`的`src`属性实现在同一页面内加载不同内容,完成页面的动态更新。
摘要由CSDN通过智能技术生成

创建index.heml

----------------------------------------------------------------------

在body中,分块

一整个顶部,命名为top

<div id="top">
    <input type="button" id="inp1" value="首页" name="首页" onclick="goPage(this)" />
    <input type="button" id="inp2" value="品牌与产品" name="品牌与产品" onclick="goPage(this)" />
    <input type="button" id="inp3" value="联营合作" name="联营合作" onclick="goPage(this)" />
    <input type="button" id="inp4" value="配套服务" name="配套服务" onclick="goPage(this)" />
    <input type="button" id="inp5" value="门店信息" name="门店信息" onclick="goPage(this)" />
    <input type="button" id="inp6" value="关于我们" onmouseover="mouseover()" onmouseleave="mouseleave()" />
</div>

--------------------------------------------------------------------------

实现下拉功能

所以,再写一个index_aboutAs

<div id="index_aboutAs">
<!--    mouseover 鼠标
        mouseover与mouseenter mouseover事件:不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件。
        mouseleave. 指点设备(通常是鼠标)的指针移出某个元素时,会触发 mouseleave 事件
-->
    <input type="button" id="inp7" value="企业" name="关于我们" onclick="goPage(this)" onmouseover="mouseover()" onmouseleave="mouseleave()" />
    <input type="button" id="inp8" value="加入我们" name="联营合作" onclick="goPage(this)" onmouseover="mouseover()" onmouseleave="mouseleave()" />
</div>

 

 ----------------------------------------------------------------------------------------------


    function mouseover() {
        const aboutAs = document.getElementById('index_aboutAs');
        aboutAs.style.display = 'block';
    }
    function mouseleave() {
        const aboutAs = document.getElementById('index_aboutAs');
        aboutAs.style.display = 'none';
    }
<!--    mouseover 鼠标
        mouseover与mouseenter mouseover事件:不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件。
        mouseleave. 指点设备(通常是鼠标)的指针移出某个元素时,会触发 mouseleave 事件
-->

 移上去就会触发,移开就什么也不会发生,如果点击,也会调用goPage函数

----------------------------------------------------------------------------------------------

goPage()函数

function goPage(obj) {
        let height;
        //如果点击的是本身,则白底,棕色字
        if (obj.name == preObj.name) {
            obj.style.color = '#65360a';
            obj.style.background = 'white';
        }
        //如果点击的不是本身,本身变为棕色底,白字
        else {
            preObj.style.color = 'white';
            preObj.style.background = '#65360a';
            obj.style.color = '#65360a';
            obj.style.background = 'white';
            preObj = obj;
        }
//如果点击的是本身,则白底,棕色字
//如果点击的不是本身,本身变为棕色底,白字

-----------------------------------------------------------------------------------------------

实现点击图片换图功能

所以再写一个对于图片的引用

<div align="center" style="margin-top: -2px;">
    <img id="img1" src="imgs/无与伦比的美味.png" width="1300px" height="600px" onclick="changeMap(this)" />
    <img id="img2" src="imgs/无与伦比的美味2.png" width="1300px" height="600px" style="display: none" onclick="changeMap(this)" />
    <img id="img3" src="imgs/无与伦比的美味3.png" width="1300px" height="600px" style="display: none" onclick="changeMap(this)" />
    <img id="img4" src="imgs/无与伦比的美味4.png" width="1300px" height="600px" style="display: none" onclick="changeMap(this)" />
</div>

 -----------------------------------------------------------------------------------------

changeMap()函数

function changeMap(obj) {
            const imgId = obj.id;
            obj.style.display = 'none';
            if (imgId == 'img1') {
                document.getElementById('img2').style.display = 'block';
            }
            if (imgId == 'img2') {
                document.getElementById('img3').style.display = 'block';
            }
            if (imgId == 'img3') {
                document.getElementById('img4').style.display = 'block';
            }
            if (imgId == 'img4') {
                document.getElementById('img1').style.display = 'block';
            }
        }

--------------------------------------------------------------------------------------------

最后写一个botton图片的引用,构建出完整的六个网页不变部分

--------------------------------------------------------------------------------------------

此时,除了top栏,都是首页.html的内容,所以可以更换别的页面,只需要更改链接

<div align="center" style="margin-top: -4px">
<!--    iframe标签是框架的一种形式,也比较常用到,iframe一般用来包含别的页面
,例如我们可以在我们自己的网站页面加载别人网站或者本站其他页面的内容。-->
    <iframe id="ifr" src="html/首页.html" width="1300px" scrolling="no" style="height: 800px; border: none;" marginwidth="0px" marginheight="0px"></iframe>
<!--此时,除了top,bottom栏,都是首页.html的内容,所以可以更换别的页面,只需要更改链接-->
</div>
<!--    iframe标签是框架的一种形式,也比较常用到,iframe一般用来包含别的页面
,例如我们可以在我们自己的网站页面加载别人网站或者本站其他页面的内容。-->

-------------------------------------------------------------------------------------------------

跟随动作,更换网页内容

//iframe标签是框架的一种形式,也比较常用到,iframe一般用来包含别的页面,
// 例如我们可以在我们自己的网站页面加载别人网站或者本站其他页面的内容。
function goPage(obj) {
        let height;
        //如果点击的是本身,则白底,棕色字
        if (obj.name == preObj.name) {
            obj.style.color = '#65360a';
            obj.style.background = 'white';
        }
        //如果点击的不是本身,本身变为棕色底,白字
        else {
            preObj.style.color = 'white';
            preObj.style.background = '#65360a';
            obj.style.color = '#65360a';
            obj.style.background = 'white';
            preObj = obj;
        }
        const page = obj.name;
        //这里在拼接url的链接
        const url = 'html/' + page + '.html';
        //更改链接,达到转换页面内容的目的
        const ifr = document.getElementById('ifr');
        //在这里本来写的是==,它提示可能因为意外类型导致,我更正==为===,避免意外类型
        if (page === '首页'){
            height = 首页;
        }
        if (page === '品牌与产品'){
            height = 品牌与产品;
        }
        if (page === '联营合作'){
            height = 联营合作;
        }
        if (page === '配套服务'){
            height = 配套服务;
        }
        if (page === '门店信息'){
            height = 门店信息;
        }
        if (page === '关于我们'){
            height = 关于我们;
        }
        //iframe标签是框架的一种形式,也比较常用到,iframe一般用来包含别的页面,
        // 例如我们可以在我们自己的网站页面加载别人网站或者本站其他页面的内容。
        ifr.style.height = height.toString() + 'px';
        ifr.src = url;
    }

起主要作用的是

const url = 'html/' + page + '.html';
//更改链接,达到转换页面内容的目的
ifr.src = url;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值