在两个弹性项目之间绘制箭头

时间:2017-02-28 04:20:00

标签: javascript jquery html css flexbox

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



$('#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')
})
&#13;
.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%;
}
&#13;
<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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:4)

您可以使用箭头为style-loader的伪元素,并将其从第一个元素中排除,在CSS中使用content

&#13;
&#13;
:not()
&#13;
$('#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')
})
&#13;
.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;
}
&#13;
&#13;
&#13;

答案 1 :(得分:2)

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

&#13;
&#13;
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')
})
&#13;
.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%;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 2 :(得分: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>

相关问题