2017-02-28 79 views
3

我希望在添加箭头时在两个弹性项目之间插入箭头(请运行代码)。必须在第一个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 –

+1

@MichaelCoker添加它作为答案。并删除最后一个 –

回答

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>

+0

非常感谢。这工作。如果我想让第一个元素的箭头也开始,我该怎么办?只是为了让我的概念清楚。 – darsh

+1

@darsh真棒,没问题。你只需要移除':not(:first-child)'的两个实例,这样选择器就会是'.flex-item {margin-left:.75em; }'和'.flex-item:before {content:'\ 21E2';} ...}像http://codepen.io/anon/pen/vxNEME –

+1

是的,我读了它,并找出它。再次感谢!你真棒:) @Michael Coker – darsh

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() { 
 
\t \t  $('#Demosss').append($('<li class="flex-item">').text('text')) 
 
\t \t  $('.enlarge').css('display','none'); 
 
\t \t  $('#Demosss').append($('<div class="enlarge1"><span>&#8674;</span></div>')) 
 
\t \t  $(this).insertAfter($('[class^="flex-item"]').last()); 
 
\t \t }); 
 

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

 
\t ul li{ 
 
\t display: inline; 
 
\t } 
 

 

 
\t .enlarge1{ 
 
\t \t zoom: 350%; 
 
\t } 
 

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

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

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

 

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

+0

这是另一种使用jQuery的方式。谢谢。顺便说一句你的代码需要一些编辑。 – darsh