【问题标题】:Cannot add custom cursor to sortable (jQuery, CSS)无法将自定义光标添加到可排序(jQuery,CSS)
【发布时间】:2016-10-11 05:25:42
【问题描述】:

我正在尝试自定义可排序对象的光标,但没有成功。但是,我可以将光标更改为“移动”。在 css 中,我有 cursor: url(../images/cursor.png) 并且浏览器成功找到了图像。对于所有其他元素,我能够将图像添加到光标,但不能用于可排序。下面是我的可排序代码。基本上我在两列中有元素,我可以将元素从一列拖到另一列,反之亦然。

var categoriesCombArr = [1, 2, 3, 4, 5 , 6, 7, 8];
var category1Arr = [1, 2, 3, 4];


$(function() {
    $("#column1, #column2").sortable({
        connectWith: ".column", 
    });
});



for (i = 0; i < categoriesCombArr.length; i++) {
        if (i < category1Arr.length) {
            $("#column1").append("<div class='choice'><div class = 'image' id = 'choice" + i + "'></div><div class = 'imageDescription'><p>" + categoriesCombArr[i] + "</p></div> </div>");
        } else {
            $("#column2").append("<div class='choice'><div class = 'image' id = 'choice" + i + "'></div><div class = 'imageDescription'><p>" + categoriesCombArr[i] + "</p></div> </div>");
        }

    }
.choice {
    position: relative;
    width: 100px;
    height: 50px;
    
    display: inline-block;
    font-size: 20px;
    cursor: pointer;
    text-align: center;
    text-decoration: none;
    outline: none;
    border: none;
    margin: auto;
    font-family: SansPro-Regular;
    border-radius: 10px;
    background-color: rgb(181, 152, 113);
    margin-top: 7px;
    cursor: url(../images/cursor.png) !important;
}

.choice:hover {
    background-color: #bd7737;
    box-shadow: inset 0px 5px 8px 0px rgba(43, 27, 0, 0.34);
    cursor: url(../images/cursor.png) !important;
}




#column1{
    position: relative;
    width: 100px;
    height: 500px;
    float: left;
    /*! left: 200px; */
    text-align: center;
    
    
    top: 19px;
    overflow: scroll;
    left: 35px;
}

#column2{
    width: 100px;
    height: 500px;
    position: relative;
    float: right;   
    /*! right: 200px; */
    
    text-align: center;
    left: -36px;
    overflow: scroll;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>

<div class = "columnsCollection"> 
	<!-- column1 -->
	<div>
	   <div id='column1' class = "column">
	   <!-- ALL CONTENT appears here -->
	   </div>
	</div>
        <!-- column1 ends here -->

	<div class="progress-bar"></div>

	<!-- column2 -->
	<div>
	   <div id = 'column2' class= 'column'>
	   <!-- ALL CONTENT appears here -->
	   </div>
	</div>
	<!-- column2 ends here -->

</div>
<!-- columnsCollection ends heres -->

【问题讨论】:

  • 尝试使用cursor: url(../images/cursor.png)!important;也尝试在这里发布html代码
  • 我认为您需要展示更多代码。 HTML 是什么样的?您使用的是什么 CSS 选择器等。
  • @AtalKishore 已经尝试过,但没有用
  • @3ocene 添加了我所有的代码。可以看看吗?
  • jsfiddle.net 是一个有用的网站。它允许您将所有代码分组到一个可以运行的示例中。这样,像我这样懒惰的人更有可能提供帮助。或者你可以像 Albert Font 在他的回答中所做的那样使用 sn-p。

标签: jquery css cursor jquery-ui-sortable


【解决方案1】:

我只是在查看this article 时偶然发现了答案。看起来当您使用来自 URL 的自定义光标时,您还需要从内置光标中指定一个备用光标,如下所示:

cursor: url(../images/cursor.png), move;

这大概是为了让浏览器知道在图像不可用时使用什么。

您还可以指定一个光标列表,浏览器将使用第一个有效的光标:

cursor: url(unavailable.png), url(../images/cursor.png), move;

【讨论】:

  • 该死的,伙计,你是个男人!非常感谢!添加“移动”就可以了
【解决方案2】:

你能看一下这段代码吗,也许它对你有帮助。

问候,

$( function() {
    $( "#sortable" ).sortable({
      cancel: ".noSortable"
    });
   
    $( "#sortable" ).disableSelection();
  } );
body {
	font-family: Arial, Helvetica, sans-serif;
}

table {
	font-size: 1em;
}

.ui-draggable, .ui-droppable {
	background-position: top;
}

#sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
  #sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px;cursor:move; }
#sortable li.noSortable{cursor:no-drop;}
  #sortable li span { position: absolute; margin-left: -1.3em; }
<link href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<ul id="sortable">
  <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 1</li>
  <li class="ui-state-default noSortable">Item 2</li>
  <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 3</li>
  <li class="ui-state-default noSortable">Item 4</li>
  <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 5</li>
  <li class="ui-state-default noSortable">Item 6</li>
  <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 7</li>
</ul>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

【讨论】:

  • 当您回答问题时,请尝试添加有关您在做什么以及为什么做的更多信息。在答案中转储代码几乎(但不完全)与在问题 imo 中转储代码一样糟糕。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-12
  • 1970-01-01
  • 2018-05-10
  • 1970-01-01
  • 2018-01-10
  • 2018-02-26
  • 2016-11-05
相关资源
最近更新 更多