简体   繁体   English

如何在交换后将列表元素返回到其原始样式

[英]How to return a list element to its original styles after a swap

I want to apologize if I wasn't very clear in the question's title, so I will explain it here with examples. 如果我在问题的标题中不是很清楚,我想道歉,所以我将在这里用例子解释。

I have this website http://color.ninski.ro where I'm trying to build a menu. 我有这个网站http://color.ninski.ro ,我正在尝试建立一个菜单。

I need two things from this menu: 我需要从这个菜单中选择两件事:

  1. When I click an element, it's position switches with the first element position (this problem was solved thanks to Khawer Zeshan a user from this website). 当我点击一个元素时,它的位置会切换到第一个元素位置(这个问题由于来自这个网站的用户Khawer Zeshan而得到解决)。
  2. After an element becomes the first, I need it to increase its width and height without pushing the rest of the elements, so it can display a "hidden text", but if you click another element, it switches it's position with the first and it returns to the original CSS. 在元素成为第一个之后,我需要它来增加其宽度和高度而不推动其余元素,因此它可以显示“隐藏文本”,但是如果你单击另一个元素,它会用第一个元素切换它的位置返回原始CSS。

This is the menu list: 这是菜单列表:

    <ul class="unstyled" id="java">
      <li id="b1"><div><a href="">Picturi Interior</a></div></li>
      <li id="b2"><div><a href="">Modele Propuse</a></div></li>
      <li id="b3"><div><a href="">Design Interior</a></div></li>
      <li id="b4"><div><a href="">Web Design</a></div></li>
      <li id="b5"><div><a href="">Obiecte Decorative</a></div></li>
   </ul>

This is the switching script if it helps you : 这是切换脚本,如果它可以帮助您:

$.fn.exchangePositionWith = function(selector) {
    var other = $(selector);
    this.after(other.clone());
    other.after(this).remove();
};
$(document.body).on('click', 'ul#java li' ,function(){
    var pos = $(this).closest('li').index();
    $("ul#java li:eq("+pos+")").exchangePositionWith("ul#java li:eq(0)");
});

This is the original CSS for the list: 这是列表的原始CSS:

#java li{
 display:inline-block;
 width:149px;
 height:67px;
 margin-left:13px;
 padding-top:40px;
 text-align:center;
}

If you think my question or my explanation is incomplete, please feel free to ask me. 如果您认为我的问题或我的解释不完整,请随时问我。

You could try: 你可以尝试:

var iniHTML = '<ul class="unstyled" id="java">';
iniHTML += '<li id="b1"><div><a href="">Picturi Interior</a></div></li>';
iniHTML += '<li id="b2"><div><a href="">Modele Propuse</a></div></li>';
iniHTML += '<li id="b3"><div><a href="">Design Interior</a></div></li>';
iniHTML += '<li id="b4"><div><a href="">Web Design</a></div></li>';
iniHTML += '<li id="b5"><div><a href="">Obiecte Decorative</a></div></li>';
iniHTML += '</ul>';

Now use jQuery's native $('wherever').html(iniHTML); 现在使用jQuery的原生$('wherever').html(iniHTML); . In other words, store the initial state of your <ul> and store a jQuery .css() Object in another variable, like: 换句话说,存储<ul>的初始状态并将jQuery .css()对象存储在另一个变量中,如:

var iniCSS = {
  width: '300px',
  height: '25px',
  color: '#000',
}
$('wherever').css(iniCSS);

I removed the divs and a-tags when tested the following; 在测试以下内容时,我删除了div和a-tags; not sure why they are there. 不知道为什么他们在那里。 In particular, I wouldn't use a-tags if they are not links, just style the li to look like a link if this is required. 特别是,如果它们不是链接,我不会使用a-tags,只需将li设置为类似于链接的样式(如果需要)。

Anyway, increasing the font-size or weight doesn't push the other elements. 无论如何,增加字体大小或重量不会推动其他元素。 There is also no need to store the previous values, as setting these to '' will revert to their original values. 也无需存储以前的值,因为将这些值设置为''将恢复为原始值。

$.fn.exchangePositionWith = function(selector) {
    var other = $(selector);
    $(other).css('fontSize','');
    this.after(other.clone());
    other.after(this).remove();
    $(this).css('fontSize','large');
};

There is now an issue if you click the first item, as its font-size will change. 如果单击第一个项目,则会出现问题,因为其字体大小将更改。 This could be accounted for in the code by checking the index of the clicked item. 这可以通过检查所单击项的索引来在代码中解决。

If you specifically want to increase the height and width of the container element then you need to use positioning to take them out of the normal flow, so that they can overlap, or keep the DIVs and alter their margins and/or padding in a similar way to my code above (using parent(), probably). 如果您特别想要增加容器元素的高度和宽度,那么您需要使用定位将它们从正常流中取出,以便它们可以重叠,或保持DIV并改变它们的边距和/或填充类似上面我的代码的方式(可能使用parent())。 (I assume you to intend to add borders or a background, otherwise the height/width increase wouldn't be needed.) (我假设您打算添加边框或背景,否则不需要增加高度/宽度。)

The following will ignore a click on the first item. 以下将忽略对第一个项目的单击。 You'll need to modify if you want this click to put the item back in its original position. 如果您希望此次点击将项目放回原始位置,则需要修改。

$('body').on('click', 'ul#java li' ,function (){
    var pos = $(this).closest('li').index();
    if (pos > 0) {
        $("ul#java li:eq("+pos+")").exchangePositionWith("ul#java li:eq(0)");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM