如何保留滚动位置? [英] How to retain the scroll position ?
问题描述
在Panel中我有FileUpload并在'show'按钮上单击我可以通过每次都关闭滚动条来查看所选图像。如何阻止垂直滚动条向上移动。我尝试使用以下代码但没有用。
< script type =text / javascript>
var FormHandlerProto = function(){
var Me = this;
this.lastScrollPos = 0;
this.SetupForm = function(){
//将函数绑定到表单的滚动容器
$(# Panel117)。bind(scroll,function(){
//记录滚动位置
Me.lastScrollPos = $(this).scrollTop();
});
}
this.ScrollForm = function(){
//应用最后滚动位置
$(#Panel117)。scrollTop(Me.lastScrollPos);
}
this.EndRequestHandler = function(sender,args){
if(args.get_error()!= undefined)
Me.ScrollForm();
}
}
}
var FormHandler = new FormHandlerProto();
FormHandler.Setup(); //这假设您的滚动容器未在回发时更新。如果是,你需要在EndRequestHandler中调用它。
Sys.WebForms.PageRequestManager.getInstance()。add_endRequest(FormHandler.EndRequestHandler);
< / script>
< asp:Panel ID = Panel117 runat = server
样式 = border-widt h:1px;边框式:坚固;上:1475px;左:125px;位置:绝对;身高:161px;宽度:279px;背景色:InactiveBorder; border-color:黑色 >
< asp:图像 ID = ImagePreview runat = server < span class =code-attribute>高度 = 97px 宽度 = 99px
< span class =code-attribute> style = z-index:1 / >
< asp:FileUpload ID = PhotoUpload < span class =code-attribute> runat = server
style = z-index:1;左:100px的;上:53px;位置:绝对;身高:46px;宽度:218px;右:90px; / >
< asp:UpdatePanel ID = UpdatePanel5 runat = server >
< ContentTemplate >
< asp:按钮 ID = Button7 runat = server CausesValidation = 错误 onclick = Showbutton_Click
style = z-index:1;左:7px;上:123px;位置:绝对 文本 = 显示 / >
< / ContentTemplate >
< 触发器 >
< span class =code-keyword>< asp:PostBackTrigger ControlID = Button7 / >
< / Triggers >
< / asp:UpdatePanel >
< / asp:Panel >
(#Panel117)。bind(scroll,function (){
//记录滚动位置
Me.lastScrollPos =
(this).scrollTop();
});
}
this.ScrollForm = function(){
//应用最后一个滚动位置
(#Panel117)。scrollTop(Me.l astScrollPos);
}
this.EndRequestHandler = function(sender,args){
if(args) .get_error()!= undefined)
Me.ScrollForm();
}
}
} < br $>
var FormHandler = new FormHandlerProto();
FormHandler.Setup(); //这假设您的滚动容器未在回发时更新。如果是,你需要在EndRequestHandler中调用它。
Sys.WebForms.PageRequestManager.getInstance()。add_endRequest(FormHandler.EndRequestHandler);
< / script>
< asp:Panel ID = Panel117 runat = server
样式 = border-widt h:1px;边框式:坚固;上:1475px;左:125px;位置:绝对;身高:161px;宽度:279px;背景色:InactiveBorder; border-color:黑色 >
< asp:图像 ID = ImagePreview runat = server < span class =code-attribute>高度 = 97px 宽度 = 99px
< span class =code-attribute> style = z-index:1 / >
< asp:FileUpload ID = PhotoUpload < span class =code-attribute> runat = server
style = z-index:1;左:100px的;上:53px;位置:绝对;身高:46px;宽度:218px;右:90px; / >
< asp:UpdatePanel ID = UpdatePanel5 runat = server >
< ContentTemplate >
< asp:按钮 ID =Button7 runat = server CausesValidation = 错误 onclick = Showbutton_Click
style = z-index:1;左:7px;上:123px;位置:绝对 文本 = 显示 / >
< / ContentTemplate >
< 触发器 >
< span class =code-keyword>< asp:PostBackTrigger ControlID = Button7 / >
< / Triggers >
< / asp:UpdatePanel >
< / asp:Panel > 跨度>
In a Panel I have FileUpload and on 'show' button click I am able to see the selected image by bringing down the scroll bar every time. How to arrest the vertical scroll bar from moving upwards. I tried with the following code but no use.
<script type="text/javascript">
var FormHandlerProto = function() {
var Me = this;
this.lastScrollPos = 0;
this.SetupForm = function() {
// Bind a function to the form's scroll container
$("#Panel117").bind("scroll", function() {
// Record the scroll position
Me.lastScrollPos = $(this).scrollTop();
});
}
this.ScrollForm = function() {
// Apply the last scroll position
$("#Panel117").scrollTop(Me.lastScrollPos);
}
this.EndRequestHandler = function(sender, args) {
if (args.get_error() != undefined)
Me.ScrollForm();
}
}
}
var FormHandler = new FormHandlerProto();
FormHandler.Setup(); // This assumes your scroll container doesn't get updated on postback. If it does, you'll want to call it in the EndRequestHandler.
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(FormHandler.EndRequestHandler);
</script>
<asp:Panel ID="Panel117" runat="server"
style="border-width: 1px; border-style: solid; top: 1475px; left: 125px; position: absolute; height: 161px; width: 279px; background-color:InactiveBorder; border-color:Black">
<asp:Image ID="ImagePreview" runat="server" Height="97px" Width="99px"
style="z-index: 1"/>
<asp:FileUpload ID="PhotoUpload" runat="server"
style="z-index: 1; left:100px; top: 53px; position: absolute; height: 46px; width: 218px; right: 90px;" />
<asp:UpdatePanel ID="UpdatePanel5" runat="server" >
<ContentTemplate>
<asp:Button ID="Button7" runat="server" CausesValidation="False" onclick="Showbutton_Click"
style="z-index: 1; left: 7px; top: 123px; position: absolute" Text="Show" />
</ContentTemplate>
<Triggers >
<asp:PostBackTrigger ControlID="Button7" />
</Triggers>
</asp:UpdatePanel>
</asp:Panel>
("#Panel117").bind("scroll", function() {
// Record the scroll position
Me.lastScrollPos =
(this).scrollTop();
});
}
this.ScrollForm = function() {
// Apply the last scroll position
("#Panel117").scrollTop(Me.lastScrollPos);
}
this.EndRequestHandler = function(sender, args) {
if (args.get_error() != undefined)
Me.ScrollForm();
}
}
}
var FormHandler = new FormHandlerProto();
FormHandler.Setup(); // This assumes your scroll container doesn't get updated on postback. If it does, you'll want to call it in the EndRequestHandler.
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(FormHandler.EndRequestHandler);
</script>
<asp:Panel ID="Panel117" runat="server" style="border-width: 1px; border-style: solid; top: 1475px; left: 125px; position: absolute; height: 161px; width: 279px; background-color:InactiveBorder; border-color:Black"> <asp:Image ID="ImagePreview" runat="server" Height="97px" Width="99px" style="z-index: 1"/> <asp:FileUpload ID="PhotoUpload" runat="server" style="z-index: 1; left:100px; top: 53px; position: absolute; height: 46px; width: 218px; right: 90px;" /> <asp:UpdatePanel ID="UpdatePanel5" runat="server" > <ContentTemplate> <asp:Button ID="Button7" runat="server" CausesValidation="False" onclick="Showbutton_Click" style="z-index: 1; left: 7px; top: 123px; position: absolute" Text="Show" /> </ContentTemplate> <Triggers > <asp:PostBackTrigger ControlID="Button7" /> </Triggers> </asp:UpdatePanel> </asp:Panel>
这篇关于如何保留滚动位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!