最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Changing DIV tags functionality like select-option? - Stack Overflow

programmeradmin3浏览0评论

I have the following HTML structure

<div class="categories">
    <div class="category">1</div>
    <div class="category">2</div>
    <div class="category">3</div>
</div>

I want to make this structure function like a SELECT/OPTION,(I can't change the tags) I have tried applying TOGGLE on the parent DIV, but that only opens and closes the DIV container, it doesn't change the functionality like SELECT-OPTION.

EDIT: Just changing it visually like a dropdown SELECT-OPTION type is enough.

Any help appreciated. Thank You.

I have the following HTML structure

<div class="categories">
    <div class="category">1</div>
    <div class="category">2</div>
    <div class="category">3</div>
</div>

I want to make this structure function like a SELECT/OPTION,(I can't change the tags) I have tried applying TOGGLE on the parent DIV, but that only opens and closes the DIV container, it doesn't change the functionality like SELECT-OPTION.

EDIT: Just changing it visually like a dropdown SELECT-OPTION type is enough.

Any help appreciated. Thank You.

Share Improve this question edited Jan 5, 2013 at 7:57 Subrata asked Jan 5, 2013 at 7:23 SubrataSubrata 2,2843 gold badges24 silver badges38 bronze badges 6
  • What exactly do you mean by " make this structure function like a SELECT/OPTION"? – FixMaker Commented Jan 5, 2013 at 7:29
  • Why can you not change the HTML elements to be an actual <select> containing <option>s? It's more semantic this way. Plus you could then create a fallback when the user doesn't have JavaScript. – diggersworld Commented Jan 5, 2013 at 7:34
  • @diggersworld: There's a lot of functionality behind this structure. I have just shown by simplifying it.Changing the div will change other functionalities and will include a lots of javascript change. So it's better to make it visibly like SELECT-OPTION type. – Subrata Commented Jan 5, 2013 at 7:53
  • @Lorax: By SELECT-OPTION type, I mean the dropdown SELECT-OPTION tags in HTML type look – Subrata Commented Jan 5, 2013 at 7:54
  • tympanus.net/codrops/2012/10/04/custom-drop-down-list-styling – erichste Commented Jan 5, 2013 at 8:04
 |  Show 1 more comment

5 Answers 5

Reset to default 6

You can try this: http://jsfiddle.net/VRjfm/

$('.categories').prepend('<span>select options</span>');

$('.categories').click(function(){
  $('div',this).slideToggle();
  $('.categories span').html('select options');
});

$('.category').click(function(e){
  e.stopPropagation(); // <--this will stop the event bubbling to its parent.
  $('.category').slideToggle();
  $('.categories span').html($(this).text());
});

Use the following javascript code. Which will solve your problem

JS Bin http://jsbin.com/utoyej/43/edit

  //This is while page load showing first element
        jQuery('.category').addClass('inactive').hide();
        jQuery('.category:first').addClass('active').removeClass('inactive').show();

  //Showing all option
        jQuery('.categories').mouseover(function(){
          jQuery('.category').show();
        });

   //Showing selected option
        jQuery('.categories').mouseleave(function(){
          jQuery('.categories .inactive').hide();
        });

 //Onclick making the option active
        jQuery('.category').click(function(){
            jQuery('.category').addClass('inactive').removeClass('active');
          jQuery(this).addClass('active').removeClass('inactive');
          jQuery('.categories .inactive').hide();


        });

I have done with the following :

HTML Part :

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<div id="dd" class="select-wrapper">--- Select ---
<div class="categories">
 <div class="category">1</div>
 <div class="category">2</div>
 <div class="category">3</div>
</div>

CSS Part :

.select-wrapper {
    /* Size & position */
    position: relative;
    width: 200px;
    margin-top: 25px;
    margin-left: 25px;
    padding: 12px 15px;

    /* Styles */
    background: #fff;
    border-radius: 5px;
    box-shadow: 0 1px 0 rgba(0,0,0,0.2);
    cursor: pointer;
    outline: none;
    -webkit-transition: all 0.3s ease-out;
    -moz-transition: all 0.3s ease-out;
    -ms-transition: all 0.3s ease-out;
    -o-transition: all 0.3s ease-out;
    transition: all 0.3s ease-out;
}

.select-wrapper:after { /* Little arrow */
    content: "";
    width: 0;
    height: 0;
    position: absolute;
    top: 50%;
    right: 15px;
    margin-top: -3px;
    border-width: 6px 6px 0 6px;
    border-style: solid;
    border-color: #4cbeff transparent;
}

.select-wrapper .categories {
    /* Size & position */
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;

    /* Styles */
    background: #fff;
    border-radius: 0 0 5px 5px;
    border: 1px solid rgba(0,0,0,0.2);
    border-top: none;
    border-bottom: none;
    list-style: none;
    -webkit-transition: all 0.3s ease-out;
    -moz-transition: all 0.3s ease-out;
    -ms-transition: all 0.3s ease-out;
    -o-transition: all 0.3s ease-out;
    transition: all 0.3s ease-out;

    /* Hiding */
    max-height: 0;
    overflow: hidden;
}


.select-wrapper .categories div {
    display: block;
    text-decoration: none;
    color: #333;
    padding: 10px;
    transition: all 0.3s ease-out;
    border-bottom: 1px solid #e6e8ea;
}


/* Hover state */

.select-wrapper .categories div:hover {
    color: #57a9d9;
}

/* Active state */

.select-wrapper.active {
    border-radius: 5px 5px 0 0;
    background: #4cbeff;
    box-shadow: none;
    border-bottom: none;
    color: white;
}

.select-wrapper.active:after {
    border-color: #82d1ff transparent;
}

.select-wrapper.active .categories {
    border-bottom: 1px solid rgba(0,0,0,0.2);
    max-height: 400px;
}

.select-wrapper:focus {
    border-radius: 5px 5px 0 0;
    background: #4cbeff;
    box-shadow: none;
    border-bottom: none;
    color: white;
}

.select-wrapper:focus:after {
    border-color: #82d1ff transparent;
}

.select-wrapper:focus .categories {
    border-bottom: 1px solid rgba(0,0,0,0.2);
    max-height: 400px;
}

jQuery Part :

function DropDown(el) {
        this.dd = el;
        this.initEvents();
    }
    DropDown.prototype = {
        initEvents : function() {
            var obj = this;

            obj.dd.on('click', function(event){
                $(this).toggleClass('active');
                event.stopPropagation();
            }); 
        }
    }

    $(function() {

        var dd = new DropDown( $('#dd') );

        $(document).click(function() {
            // all dropdowns
            $('.select-wrapper').removeClass('active');
        });

    });

To see it in action : http://jsfiddle.net/john_rock/LhUsc/

I think this may help you to resolve your problem.

You can add a click event on the inner ('.catefory') divs and there capture the "selected" value.

ex:

var selectedValue = '';

$('.category').click(function () {
  selectedValue = $(this).text();
  $('.categories').hide(); /* if you want to hide the options panel */ 
});

jsFiddle DEMO

EDIT: I revised the jsFiddle to show custom label after the div has a new selection.

For a pure JavaScript solution, this jsFiddle uses a simple plugin that has excellent animation effects and configuration settings.

HTML:

  <div class="categories" id="examplePanel1" style="position:absolute; width:150px; height:75px; top:20px; left:0px; background:#a6bbcd; text-align:center; overflow:hidden;">
    <div class="category" onclick="slideExample1('examplePanel1', 'exampleHeader1'); resetLabel1(); dark(); return false;">1 - Dark</div>
    <div class="category" onclick="slideExample1('examplePanel1', 'exampleHeader1'); resetLabel1(); light(); return false;">2 - Light</div>
    <div class="category" onclick="slideExample1('examplePanel1', 'exampleHeader1'); resetLabel1(); image(); return false;">3 - Image</div>
  </div>

JavaScript:

// Div 1 choice: Show purple color via image.
function light() {
  document.getElementById('theBox').style.backgroundImage = "url(http://placehold.it/100x100/FFFF99&text=Light)";
}

// Div 2 choice: Show yellow color via image.    
function dark() {
  document.getElementById('theBox').style.backgroundImage = "url(http://placehold.it/100x100/333399&text=Dark)";
}

// Div 3 choice: Show image
function image() {
  document.getElementById('theBox').style.backgroundImage = "url(http://www.gravatar.com/avatar/66cc4497ef4e7a711a1f83e6a74cfea1?s=100&d=identicon&r=PG)";
}

Original Tutorial
Panel Animation and Tutorial

If you like the jsFiddle, please do check out the Panel Animation link to see many different types of animations just by configuration.

发布评论

评论列表(0)

  1. 暂无评论