2014-12-13 65 views
0

當頁面滾動時,需要保持對象位於頂部。我在How to keep object on page scrolling with the top of the page上找到了這個例子。但是當頁面滾動時,對象改變寬度。有人可以幫助我如何解決問題。在此先感謝如何修復頁面滾動時的對象寬度

當頁面加載;它在fieldset中的對象。 enter image description here

滾動頁面後,對象改變寬度。 enter image description here

有我的代碼:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm2.aspx.vb"  
    Inherits="myProject.WebForm2" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
     Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
    <head runat="server"> 
    <title></title> 

    <script language="javascript" src="/include/jquery.js" type="text/javascript"></script> 
    <style type="text/css"> 

     #scroller { 
      position: relative; 
      top: 0px; 
      width: 90%; 
      background: GREY; 
      height: 120px; 
      } 

     .fixed { 
     position: fixed !important; 
      /*zoom:1; /* This enables hasLayout, which is required for older IE browsers */ 
     top: 0px !important; 
     } 
</style> 

    <script type="text/jscript"> 

    $(window).scroll(function() { 
     if ($(window).scrollTop() > 100) { 
      $('#scroller').addClass('fixed'); 
     } else { 
      $('#scroller').removeClass('fixed'); 
     } 
    }); 

    </script> 


</head> 

    <body id="bdypopup" > 
    <form id="Form2" runat="server"> 
     <div id="mainpopup" style="width:90%;"> 
      <fieldset class="fldBoxy " style="background-color:Red;">    

       <legend class="fldLegend" >Edit Order      
       </legend>     
       <div id="scroller"> 
       <asp:Label ID="lblYear" runat="server" Text="TESTING"></asp:Label> 
       <table width="98%" > 
       <tr> 
       <td width="65%"> <asp:HiddenField ID="hdUnCheckRow" runat="server" /> </td> 
       <td> 
        <asp:CheckBox ID="chkOne" runat="server" Text="Show One" Checked="true" Visible="true" /> 
        <br /> 
        <asp:CheckBox ID="ChkTwo" runat="server" Text="Show Two" checked="true" Visible="true"/> 
        <br /> 

        </td> 
       </tr> 
       </table> 

       <div > 

        <asp:button id="btnSave" runat="server" text="Save" causesvalidation="true" OnClientClick="enableButton()" UseSubmitBehavior="false" />&nbsp;     
        <button id="btnCancel" type="button" runat="server" onclick="closeWindows()">Cancel</button> 

       </div> 
       </div> 
       <asp:Literal ID="ltr" runat="server" /> 
       </fieldset> 
</div> 
</form> 
</body> 
</html> 

回答

1

Demo

$(window).scroll(function() { 
      var wid = $('#scroller').outerWidth(); 
      if ($(window).scrollTop() > 100) { 
       $('#scroller').addClass('fixed').css('width', wid); 
      } else { 
       $('#scroller').removeClass('fixed'); 
      } 
     }); 

,並提高你的代碼

$(document).ready(function(){ 
    $(window).on('scroll',function() { 
      var wid = $('#scroller').outerWidth(); 
      if ($(window).scrollTop() > 100) { 
       $('#scroller').addClass('fixed').css('width', wid); 
      } else { 
       $('#scroller').removeClass('fixed'); 
      } 
     }); 
}); 
+1

感謝。我工作了。 – user819774 2014-12-15 14:32:47

+0

@ user819774祝你好運 – 2014-12-15 15:52:15

相關問題