Keyboard Image Navigation

3/16/2025

当前为 2025-03-16 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Keyboard Image Navigation
// @namespace   Violentmonkey Scripts
// @match       https://divisare.com/projects/*
// @grant       none
// @version     1.0
// @author      tjia.work
// @description 3/16/2025
// @license     MIT
// ==/UserScript==

(function() {
  'use strict';

  // Select all images in #gallery
  const images = Array.from(document.querySelectorAll("#gallery .image"));
  if (images.length === 0) return; // Exit if no images found

  const options = {
    behavior: "instant", // smooth | instant | auto
    block: "start", // start | center | end | nearest
    inline: "start", // start | center | end | nearest
  };

  let index = 0; // Track current image index

  // Set up Intersection Observer
  const observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        index = images.indexOf(entry.target);
      }
    });
  }, { threshold: 0.5 });

  images.forEach(img => observer.observe(img));

  // Keyboard navigation
  document.addEventListener("keydown", (e) => {
    switch (e.key.toLowerCase()){
      case "arrowright":
      case "d":
      case "j":
        index = Math.min(index + 1, images.length - 1);
        break;
      case "arrowleft":
      case "a":
      case "k":
        index = Math.max(index - 1, 0);
        break;
      default:
        return;
        break;
    }
    images[index].scrollIntoView(options);

  });

})();