向上滚动到iOS Safari的最顶层会导致向上/向下检测问题

时间:2019-01-23 06:40:10

标签: javascript ios safari

我有以下Javascript代码:

else

因此,它可以与非移动设备和Android设备完美配合。但是,当我在iOS的Safari上运行该代码并滚动到最顶部时,它会在放开手指时弹起并向后弹起的同时将视口向下拉一点。上面的Javascript代码检测到向上反弹,并在public class MainActivity extends AppCompatActivity { BluetoothManager btManager; BluetoothAdapter btAdapter; BluetoothLeScanner btScanner; Button startScanningButton; Button stopScanningButton; TextView peripheralTextView; private final static int REQUEST_ENABLE_BT = 1; private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); peripheralTextView = (TextView) findViewById (R.id.PeripheralTextView); peripheralTextView.setMovementMethod(new ScrollingMovementMethod()); startScanningButton = (Button) findViewById(R.id.StartScanButton); startScanningButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { startScanning(); } }); stopScanningButton = (Button) findViewById(R.id.StopScanButton); stopScanningButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { stopScanning(); } }); stopScanningButton.setVisibility(View.INVISIBLE); btManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE); btAdapter = btManager.getAdapter(); btScanner = btAdapter.getBluetoothLeScanner(); if (btAdapter != null && !btAdapter.isEnabled()) { Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent,REQUEST_ENABLE_BT); } } // Device scan callback. private ScanCallback leScanCallback = new ScanCallback() { @Override public void onScanResult(int callbackType, ScanResult result) { peripheralTextView.append("Device Name: " + result.getDevice().getName() + " rssi: " + result.getRssi() + "\n"); // auto scroll for text view final int scrollAmount = peripheralTextView.getLayout ().getLineTop (peripheralTextView.getLineCount())- peripheralTextView.getHeight(); // if there is no need to scroll, scrollAmount will be <=0 if (scrollAmount > 0) peripheralTextView.scrollTo(0, scrollAmount); } }; public void startScanning() { System.out.println("start scanning"); peripheralTextView.setText(""); startScanningButton.setVisibility(View.INVISIBLE); stopScanningButton.setVisibility(View.VISIBLE); AsyncTask.execute(new Runnable() { @Override public void run() { btScanner.startScan(leScanCallback); } }); } public void stopScanning() { System.out.println("stopping scanning"); peripheralTextView.append("Stopped Scanning"); startScanningButton.setVisibility(View.VISIBLE); stopScanningButton.setVisibility(View.INVISIBLE); AsyncTask.execute(new Runnable() { @Override public void run() { btScanner.stopScan(leScanCallback); } }); } } 段上引起了触发,这是不希望的。我该如何解决?

1 个答案:

答案 0 :(得分:1)

在Safari反弹之前,会将Y-Position过度滚动到负地带。您可以使用它忽略弹跳动画中的位置更改。

窗口滚动事件触发得非常快。出于性能原因,您不想直接处理这些事件。

下面的示例显示了如何以250ms的间隔检查用户是否滚动,这在性能上很容易。

var didScroll = false;

$(window).scroll(function() {
  didScroll = true;
});

// interval scroll handler
setInterval(function() {
  if ( didScroll ) {
    didScroll = false;
    scrolled();
  }
}, 250);

var lastScrollTop = 0;

function scrolled() {
  var scrollStatus = window.pageYOffset;

  if (scrollStatus < lastScrollTop) {
      //user scrolled up
  } else if ( lastScrollTop >= 0) {
      //user scrolled down
  }
  lastScrollTop = scrollStatus;
}

或者,您可以通过重置仅在滚动完成后才调用scrolled()函数的超时来缓解性能问题。

var timer;

$(window).scroll(function() {
  if ( timer ) clearTimeout(timer);
  timer = setTimeout(function(){
    scrolled();
  }, 100);
});

var lastScrollTop = 0;

function scrolled() {
  var scrollStatus = window.pageYOffset;

  if (scrollStatus < lastScrollTop) {
      //user scrolled up
  } else if ( lastScrollTop >= 0) {
      //user scrolled down
  }
  lastScrollTop = scrollStatus;
}