【问题标题】:CSS for grabbing cursors (drag & drop)用于抓取光标的 CSS(拖放)
【发布时间】:2011-08-07 12:17:30
【问题描述】:

我有一个 JavaScript webapp,用户需要在其中抓取背景来移动整个屏幕。所以我希望光标在悬停在背景上时发生变化。 -moz-grab-moz-grabbing CSS 游标非常适合这种情况。当然,它们只在 Firefox 中工作......是否有其他浏览器的等效光标?我必须做一些比标准 CSS 光标更自定义的事情吗?

【问题讨论】:

    标签: css mouse-cursor


    【解决方案1】:

    如果其他人偶然发现这个问题,这可能就是您要寻找的:

    .grabbable {
        cursor: move; /* fallback if grab cursor is unsupported */
        cursor: grab;
        cursor: -moz-grab;
        cursor: -webkit-grab;
    }
    
     /* (Optional) Apply a "closed-hand" cursor during drag operation. */
    .grabbable:active {
        cursor: grabbing;
        cursor: -moz-grabbing;
        cursor: -webkit-grabbing;
    }
    

    【讨论】:

    • 由于某种原因,“抓取”仅在我释放鼠标时出现。知道这是为什么吗?
    • @Jona 我的猜测是您没有将grabbable 类添加到任何可以抓取的元素中,并且当它们被拖动时您正在切换类。
    • 不错的扩展答案,感谢您添加额外的“抓取”位。很好的接触。 :)
    • 对于任何对此解决方案有问题的人,我必须将 grab 光标设置在 :hover 而不是普通选择器上,即上面示例中的 .grabbable:hover
    • @Jona 将这些样式添加到父级 <ul> 而不是 <li> 在我的情况下解决了问题
    【解决方案2】:

    我认为move 可能是最接近你正在做的事情的standard cursor value

    移动
    表示要移动的东西。

    【讨论】:

    • 我看到了移动图标,认为抓取图标更好。但是现在您指出 w3c 认为光标“指示要移动的东西”是最有意义的。谢谢。
    • @at:您可以在逗号分隔的列表中指定多个光标,并且用户代理应该使用它理解的第一个光标。所以你可以使用 -moz* 和“移动”作为后备。
    • @muistooshort 你确定逗号列表仍然有效吗?我正在使用cursor:move; cursor:-webkit-grab; cursor:-moz-grab; cursor:grab;,最喜欢最后一个。
    • @BobStein-VisiBone:我认为几年前可能会有一些混乱。 AFAIK 如果您指定多个 formatscursor: url(example.svg#linkcursor), url(hyper.cur), pointer 而不是多个可能的值,则逗号列表有效。我认为你的方法可能是必要的。
    【解决方案3】:

    CSS3 grabgrabbing 现在是 cursor 的允许值。 为了为 cross-browser compatibility3 提供多个备用方案,包括自定义光标文件,完整的解决方案如下所示:

    .draggable {
        cursor: move; /* fallback: no `url()` support or images disabled */
        cursor: url(images/grab.cur); /* fallback: Internet Explorer */
        cursor: -webkit-grab; /* Chrome 1-21, Safari 4+ */
        cursor:    -moz-grab; /* Firefox 1.5-26 */
        cursor:         grab; /* W3C standards syntax, should come least */
    }
    
    .draggable:active {
        cursor: url(images/grabbing.cur);
        cursor: -webkit-grabbing;
        cursor:    -moz-grabbing;
        cursor:         grabbing;
    }
    

    2019 年 10 月 7 日更新:

    .draggable {
        cursor: move; /* fallback: no `url()` support or images disabled */
        cursor: url(images/grab.cur); /* fallback: Chrome 1-21, Firefox 1.5-26, Safari 4+, IE, Edge 12-14, Android 2.1-4.4.4 */
        cursor: grab; /* W3C standards syntax, all modern browser */
    }
    
    .draggable:active {
        cursor: url(images/grabbing.cur);
        cursor: grabbing;
    }
    

    【讨论】:

    • 您的帖子与 J.Steve 的重复
    • @user2230470 区别主要有两点:第一,它为不支持grab的浏览器提供了一个光标图像,但提供了光标图像。其次,最好在供应商前缀值之后使用标准语法
    • 真的吗?!怎么会?另外,我在哪里可以找到有关此类标准做法的更多信息。
    • @user2230470 - 因为在浏览器支持 2 种行为的情况下,其中前缀的行为可能在标准最终确定之前稍稍实施(因此可能会采取不同的行动),您希望它使用标准一个......无论最后出现的定义是浏览器将使用的定义。因此,标准的应该放在最后。
    • images/grab.cur 一个我需要在我的网络服务器上托管的图像的示例 URL,或者是一些神奇的 IE 东西?
    【解决方案4】:

    比 CSS 游标“更自定义”意味着某种类型的插件,但您完全可以使用 CSS 指定自己的游标。我认为这个列表有你想要的:

    .alias {cursor: alias;}
    .all-scroll {cursor: all-scroll;}
    .auto {cursor: auto;}
    .cell {cursor: cell;}
    .context-menu {cursor: context-menu;}
    .col-resize {cursor: col-resize;}
    .copy {cursor: copy;}
    .crosshair {cursor: crosshair;}
    .default {cursor: default;}
    .e-resize {cursor: e-resize;}
    .ew-resize {cursor: ew-resize;}
    .grab {cursor: grab;}
    .grabbing {cursor: grabbing;}
    .help {cursor: help;}
    .move {cursor: move;}
    .n-resize {cursor: n-resize;}
    .ne-resize {cursor: ne-resize;}
    .nesw-resize {cursor: nesw-resize;}
    .ns-resize {cursor: ns-resize;}
    .nw-resize {cursor: nw-resize;}
    .nwse-resize {cursor: nwse-resize;}
    .no-drop {cursor: no-drop;}
    .none {cursor: none;}
    .not-allowed {cursor: not-allowed;}
    .pointer {cursor: pointer;}
    .progress {cursor: progress;}
    .row-resize {cursor: row-resize;}
    .s-resize {cursor: s-resize;}
    .se-resize {cursor: se-resize;}
    .sw-resize {cursor: sw-resize;}
    .text {cursor: text;}
    .url {cursor: url(https://www.w3schools.com/cssref/myBall.cur),auto;}
    .w-resize {cursor: w-resize;}
    .wait {cursor: wait;}
    .zoom-in {cursor: zoom-in;}
    .zoom-out {cursor: zoom-out;}
    <h1>The cursor Property</h1>
    <p>Hover mouse over each to see how the cursor looks</p>
    
    <p class="alias">cursor: alias</p>
    <p class="all-scroll">cursor: all-scroll</p>
    <p class="auto">cursor: auto</p>
    <p class="cell">cursor: cell</p>
    <p class="context-menu">cursor: context-menu</p>
    <p class="col-resize">cursor: col-resize</p>
    <p class="copy">cursor: copy</p>
    <p class="crosshair">cursor: crosshair</p>
    <p class="default">cursor: default</p>
    <p class="e-resize">cursor: e-resize</p>
    <p class="ew-resize">cursor: ew-resize</p>
    <p class="grab">cursor: grab</p>
    <p class="grabbing">cursor: grabbing</p>
    <p class="help">cursor: help</p>
    <p class="move">cursor: move</p>
    <p class="n-resize">cursor: n-resize</p>
    <p class="ne-resize">cursor: ne-resize</p>
    <p class="nesw-resize">cursor: nesw-resize</p>
    <p class="ns-resize">cursor: ns-resize</p>
    <p class="nw-resize">cursor: nw-resize</p>
    <p class="nwse-resize">cursor: nwse-resize</p>
    <p class="no-drop">cursor: no-drop</p>
    <p class="none">cursor: none</p>
    <p class="not-allowed">cursor: not-allowed</p>
    <p class="pointer">cursor: pointer</p>
    <p class="progress">cursor: progress</p>
    <p class="row-resize">cursor: row-resize</p>
    <p class="s-resize">cursor: s-resize</p>
    <p class="se-resize">cursor: se-resize</p>
    <p class="sw-resize">cursor: sw-resize</p>
    <p class="text">cursor: text</p>
    <p class="url">cursor: url</p>
    <p class="w-resize">cursor: w-resize</p>
    <p class="wait">cursor: wait</p>
    <p class="zoom-in">cursor: zoom-in</p>
    <p class="zoom-out">cursor: zoom-out</p>

    来源:CSS cursor Property @ W3Schools

    【讨论】:

      【解决方案5】:

      我可能会迟到,但你可以试试下面的代码,它对我的​​拖放很有用。

      .dndclass{
          cursor: url('../images/grab1.png'), auto; 
      
      }
      
      .dndclass:active {
          cursor: url('../images/grabbing1.png'), auto;
      }
      

      您可以在上面的 URL 中使用下面的图像。确保它是PNG透明图像。如果没有,请从 google 下载一个。

      【讨论】:

        【解决方案6】:

        您可以创建自己的光标并使用 cursor: url('path-to-your-cursor'); 将它们设置为光标,或者找到 Firefox 并复制它们(奖励:在每个浏览器中都有一个很好的一致外观)。

        【讨论】:

          【解决方案7】:

          闭合的手形光标不是 16x16。如果您需要它们的尺寸相同,那么您可以将它们都设置为 16x16 像素

          或者如果您需要原始光标:

          https://www.google.com/intl/en_ALL/mapfiles/openhand.cur https://www.google.com/intl/en_ALL/mapfiles/closedhand.cur

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-01-17
            • 1970-01-01
            • 2019-07-05
            • 2022-12-08
            相关资源
            最近更新 更多