我试图在div中实现水平滚动。我的问题是如何检测水平滚动的结束?
我试过这样的事情
$(function() {
var scrollLeftPrev=0;
$('#scrollquestion').scroll( function() {
var newScrollLeft=$('#scrollquestion').scrollLeft();
if(scrollLeftPrev===newScrollLeft){
alert('right end');
}
if(newScrollLeft===0){
alert('left end');
}
console.log($('#scrollquestion').width());
console.log(newScrollLeft);
scrollLeftPrev=newScrollLeft;
});
});
左端警报有效,因为对于所有设备大小,它将变为0。对于右端,它取决于设备大小。
答案 0 :(得分:14)
使用scrollWidth
和width
以及leftscrollwidth
来获得差异。在您的情况下,有8
的偏移量,因此可能会因为您的填充或边距而产生8
的差异。
var $elem=$('#scrollquestion');
var newScrollLeft = $elem.scrollLeft(),
width=$elem.width(),
scrollWidth=$elem.get(0).scrollWidth;
var offset=8;
if (scrollWidth- newScrollLeft-width==offset) {
alert('right end');
}
使用outerWidth()获取包含宽度的偏移量,
var $elem=$('#scrollquestion');
var newScrollLeft = $elem.scrollLeft(),
width=$elem.outerWidth(),
scrollWidth=$elem.get(0).scrollWidth;
if (scrollWidth-newScrollLeft==width) {
alert('right end');
}
答案 1 :(得分:6)
尝试http://jsfiddle.net/trxe4n3u/3/
{{1}}
答案 2 :(得分:2)
Rohan kumar的答案是正确的并且工作正常,但你可以在不手动计算偏移量的情况下做到这一点
Dim serverDate = DateTime.ParseExact("2015-08-18T11:09:23.0918585+01:00", "o", CultureInfo.InvariantCulture)
答案 3 :(得分:0)
对于我的具体情况,使用
的dangor解决方案using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Mouse_Test
{
public partial class Form1 : Form
{
ArrayList listOfPoints;
bool PencilDown;
public Form1()
{
InitializeComponent();
listOfPoints = new ArrayList();
PencilDown = false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left || e.Button == MouseButtons.Right)
{
Point p = new Point(e.X, e.Y);
listOfPoints.Add(p);
PencilDown = true;
this.statusStrip1.Items[0].Text = "MouseDown";
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Graphics g = this.CreateGraphics();
Point points = new Point(e.X, e.Y);
Pen pencil = new Pen(Color.White);
if (e.Button == MouseButtons.Left)
{
pencil = new Pen(Color.BlueViolet);
}
if(e.Button == MouseButtons.Right)
{
pencil = new Pen(Color.Red);
}
if(PencilDown)
{
this.statusStrip1.Items[0].Text = "MouseMove";
if(listOfPoints.Count > 1)
{
g.DrawLine(pencil, (Point)listOfPoints[listOfPoints.Count - 1], points);
}
listOfPoints.Add(points);
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
PencilDown = false;
this.statusStrip1.Items[0].Text = "MouseUp";
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
}
}
}
对我不起作用,因为计算($ scrollWidth-$ width)创建了一个很小的小数,使比较的左侧为浮点数,而右侧($ scrollLeft)为整数。将其更改为下面的代码对我来说非常合适。
if ($scrollWidth - $width === $scrollLeft) {
alert('right end');
}