在两个 flex 项目之间画箭头

3

当添加两个flex项目时,我希望箭头插入它们之间(请运行代码)。对于它们之间添加的每个flex项目,必须在第一个flex项目之后添加它,以便它们通过箭头连接。箭头必须位于框边界的中点处(而不是框中心)连接它们。

$('#clickMe').click(function() {
    $('#Demosss').append($('<li  class="flex-item">').text('text'))
    $(this).insertAfter($('[class^="flex-item"]').last());
});

$(document).on('click', '.flex-item' ,function(){
  $(this).toggleClass('flexActive')
})
.flex-container {
    padding: 0;
    margin: 0;
    list-style: none;
    -webkit-flex-direction: row; /* Safari */
    flex-direction:  row;
    flex-wrap:  wrap;
}
.flex-item {
    background: green;
    padding: 5px;
    width: 100px;
    height: 150px;
    margin-top: 10px;
    margin-right: 15px;
    line-height: 150px;
    color: white;
    font-weight: bold;
    font-size: 3em;
    text-align: center;
    display: inline-block;
    
}
.flexActive{
  width:auto;
  display:block;
  flex: 1 1;
  margin-right:0;
}

ul li{
  display: inline;
}




.enlarge{
 zoom: 350%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="Demosss" class="flex-container">

<!-- add LI here -->
</ul>


<button id="clickMe">Click Me</button>

<div class="enlarge">
<span>&#8674;</span>
</div>


4
像这样吗?http://codepen.io/anon/pen/vxNEjo - Michael Coker
1
@MichaelCoker 把它作为答案添加,并删除最后一个。 - Alive to die - Anant
3个回答

4
你可以使用带有箭头的伪元素作为content,并在CSS中使用:not()来排除第一个元素。

$('#clickMe').click(function() {
    $('#Demosss').append($('<li  class="flex-item">').text('text'))
    $(this).insertAfter($('[class^="flex-item"]').last());
});

$(document).on('click', '.flex-item' ,function(){
  $(this).toggleClass('flexActive')
})
.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  -webkit-flex-direction: row;
  /* Safari */
  flex-direction: row;
  flex-wrap: wrap;
}

.flex-item {
  background: green;
  padding: 5px;
  width: 100px;
  height: 150px;
  margin-top: 10px;
  margin-right: 15px;
  line-height: 150px;
  color: white;
  font-weight: bold;
  font-size: 3em;
  text-align: center;
  display: inline-block;
  position: relative;
}

.flexActive {
  width: auto;
  display: block;
  flex: 1 1;
  margin-right: 0;
}

ul li {
  display: inline;
}

.flex-item:not(:first-child) {
  margin-left: .75em;
}

.flex-item:not(:first-child):before {
  content: '\21E2';
  color: black;
  position: absolute;
  top: 50%;
  left: 0;
  transform: translate(-100%, -50%);
  z-index: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="Demosss" class="flex-container"></ul>

<button id="clickMe">Click Me</button>


非常感谢。这个方法有效。如果我想让箭头也指向第一个元素,该怎么做呢?只是为了让我的概念更清晰。 - HiddenMarkow
1
@darsh 太棒了,没问题。你只需要删除两个 :not(:first-child) 的实例,这样选择器就会变成 .flex-item { margin-left: .75em; }.flex-item:before { content: '\21E2'; ... } 就像这个 http://codepen.io/anon/pen/vxNEME。 - Michael Coker
1
是的,我已经阅读了相关资料并理解了。再次感谢你!你真棒:)@Michael Coker - HiddenMarkow

2

去掉块元素之间的水平边距,然后在检测到至少添加了一个块时,在每个插入的块前有条件地附加箭头。

var first = true

$('#clickMe').click(function() {
    var demos = $('#Demosss')
    if (!first) {
      demos.append('&#8674;')
    } else first = false
    demos.append($('<li class="flex-item">').text('text'))
    $(this).insertAfter($('.flex-item').last());
});

$(document).on('click', '.flex-item', function (){
  $(this).toggleClass('flexActive')
})
.flex-container {
    padding: 0;
    margin: 0;
    list-style: none;
    -webkit-flex-direction: row; /* Safari */
    flex-direction:  row;
    flex-wrap:  wrap;
}
.flex-item {
    background: green;
    padding: 5px;
    width: 100px;
    height: 150px;
    margin-top: 10px;
    line-height: 150px;
    color: white;
    font-weight: bold;
    font-size: 3em;
    text-align: center;
    display: inline-block;
    
}
.flexActive{
  width:auto;
  display:block;
  flex: 1 1;
  margin-right:0;
}

ul li{
  display: inline;
}




.enlarge{
 zoom: 350%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="Demosss" class="flex-container">

<!-- add LI here -->
</ul>


<button id="clickMe">Click Me</button>

<div class="enlarge">
</div>


1
尝试做类似于这样的事情。

$('#clickMe').click(function() {
      $('#Demosss').append($('<li  class="flex-item">').text('text'))
      $('.enlarge').css('display','none');
      $('#Demosss').append($('<div class="enlarge1"><span>&#8674;</span></div>'))
      $(this).insertAfter($('[class^="flex-item"]').last());
  });

  $(document).on('click', '.flex-item' ,function(){
    $(this).toggleClass('flexActive')
  })
.flex-container {
     padding: 0;
     margin: 0;
     list-style: none;
     -webkit-flex-direction: row; /* Safari */
     flex-direction:  row;
     flex-wrap:  wrap;
 }
 .flex-item {
     background: green;
     padding: 5px;
     width: 100px;
     height: 150px;
     margin-top: 10px;
     margin-right: 15px;
     line-height: 150px;
     color: white;
     font-weight: bold;
     font-size: 3em;
     text-align: center;
     display: inline-block;
     
 }
 .flexActive{
   width:auto;
   display:block;
   flex: 1 1;
   margin-right:0;
 }

 ul li{
   display: inline;
 }


 .enlarge1{
  zoom: 350%;
 }

 .enlarge{
  zoom: 350%;
 }
<!-- jQuery library -->
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

 <!-- Latest compiled JavaScript -->
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<ul id="Demosss" class="flex-container">

 <!-- add LI here -->
 </ul>


 <button id="clickMe">Click Me</button>
 <div class="enlarge"><span>&#8674;</span></div>
 <div class="arrow">
 </div>


这是另一种使用jQuery的方法。谢谢。顺便说一下,你的代码需要进行一些编辑。 - HiddenMarkow

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接