下面是聊天脚本。每当我试图向上拖动时,滚动条都会向下拖动。如何允许在我的以下代码中拖动。
有没有其他方法可以让我的代码更好,并允许滚动。
default.aspx
<!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>
</head>
<body>
<form id="form1" runat="server">
<div id="div1" style="height:400px; width:400px; overflow:auto; z-index:1">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<div id="div2" style="height:300px; width:350px">
<asp:BulletedList ID="BulletedList1" runat="server" />
</div>
<div id="div4" style="position:absolute; left:500px; bottom:50px; z-index:10">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<div id="div5" style="position:absolute; left:100px; bottom:50px; z-index:10">
<asp:TextBox ID="TextBox1" runat="server"/>
</div>
</form>
<script type="text/javascript">
function _SetChatTextScrollPosition() {
var chatText = document.getElementById("div1");
chatText.scrollTop = chatText.scrollHeight;
window.setTimeout("_SetChatTextScrollPosition()", 1);
}
window.onload = function () {
_SetChatTextScrollPosition();
}
</script>
</body>
</html>服务器代码
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Timer1_Tick(object sender, EventArgs e)
{
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=chatserver;" + "UID=root;" + "PASSWORD=******;" + "OPTION=3";
OdbcConnection MyConnection = new OdbcConnection(MyConString);
MyConnection.Open();
OdbcCommand cmd = new OdbcCommand("Select message from shoutbox", MyConnection);
OdbcDataReader dr = cmd.ExecuteReader();
ArrayList values = new ArrayList();
while (dr.Read())
{
string ep = dr[0].ToString();
values.Add(new PositionData(ep));
BulletedList1.DataSource = values;
BulletedList1.DataTextField = "Message";
BulletedList1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=chatserver;" + "UID=root;" + "PASSWORD=******;" + "OPTION=3";
OdbcConnection MyConnection = new OdbcConnection(MyConString);
OdbcCommand cmd = new OdbcCommand("INSERT INTO shoutbox(name, message)VALUES(?, ?)", MyConnection);
cmd.Parameters.Add("@name", OdbcType.VarChar, 255).Value = "gimp";
cmd.Parameters.Add("@message", OdbcType.Text).Value = TextBox1.Text;
MyConnection.Open();
cmd.ExecuteNonQuery();
MyConnection.Close();
}
}
public class PositionData
{
private string name;
public PositionData(string name)
{
this.name = name;
}
public string Message
{
get
{
return name;
}
}
}发布于 2011-10-01 20:20:27
我认为解决方案将是检测浏览器窗口是否正在被用户滚动。如果是,则不要设置滚动位置,否则请滚动div元素。
Javascript更改
var isScrolling;
document.observe('user:scrolling', function() { isScrolling = true; });
function _SetChatTextScrollPosition() {
if(!isScrolling) {
var chatText = document.getElementById("div1");
chatText.scrollTop = chatText.scrollHeight;
window.setTimeout("_SetChatTextScrollPosition()", 1);
}
isScrolling = false;
}超文本标记语言更改
<body onscroll="document.fire('user:scrolling')">Reference link to detect the browser being window scrolled
希望这对你有帮助。
感谢并致以问候
苛刻的贝德。
发布于 2011-09-26 22:58:28
因此,如果你想能够向上滚动,你要么必须停止使用这个功能,要么将超时间隔设置为更长的时间(以毫秒为单位,所以1000 == 1秒),这样你至少有机会在它将你踢回底部之前滚动和查看。
https://stackoverflow.com/questions/7538005
复制相似问题