GridView 始终在上面

本文介绍如何使用JavaScript让GridView控件始终保持在网页顶部,即使用户滚动页面也是如此。通过具体代码示例,展示了不同浏览器环境下实现这一效果的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Introduction: Recently, I was working on a project that required that certain control always stay at the top of the page even if the user scrolls down the page. In this article I will demonstrate how to keep the GridView control at the top of the page using JavaScript. Scrolling Code: I got this neat scrolling code from the website known as codepunk. You can check out the original article at this link. The author did a very good job in explaining the concepts behind scrolling. Let’s check out the JavaScript code: function scrollingDetector(){ if (navigator.appName == "Microsoft Internet Explorer") {           if(!document.documentElement.scrollTop)     document.getElementById("myDiv").style.top = document.body.scrollTop;      else      document.getElementById("myDiv").style.top = document.documentElement.scrollTop;  }   else{        document.getElementById("myDiv").style.top = window.pageYOffset + "px";    }  } The scrollingDetector method is used to assign the coordinates to the Div control with the ID is “myDiv”. The Div control contains the GridView control. The line navigator.appName checks that the browser type of the user. This check is made because different browsers use different properties to interact with the control. The above script will return you the position of the scroll bar. But, we need to call it repeatedly so the Div can change its position. For this reason I have created a new method called startScrollingDetector(). This method calls the JavaScript function setInterval which in turns call a any method at regular intervals. In the code below the function scrollingDetector() is called repeatedly after every 5 seconds. function startScrollingDetector()  {     setInterval("scrollingDetector()",5000); } The GridView Code: The GridView HTML code is pretty simple, as all you need to do is to place the GridView inside the “myDiv” control. <div id="myDiv" style="position:absolute; top:100px; right:100px;" >    <asp:GridView ID="GridView1" runat="server" CellPadding="4" Font-Names="Verdana"        ForeColor="#333333" GridLines="None">        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />        <AlternatingRowStyle BackColor="White" />    </asp:GridView> </div> You can check out the animation effect below. Please keep in mind that for animation effect I have decreased the interval to one second instead of five seconds.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值