简体   繁体   中英

why transition not working in margin-left property

could you please tell me why transition property is working in margin-left property.Actually on button click i am shifting margin-left property .I need some animation .

I can do with help of jQuery using animate function .But I want to do with css

i try like this

.outer li {
    display: inline-block;
    -webkit-transition: margin-left 2s; /* Safari */
    transition: margin-left 2s;
}

JS

function addToMarginLeft(elem, pixels) {
    var ml = parseFloat(elem.css('margin-left'));
   /* elem.animate({
    'margin-left': (ml + pixels) + 'px'
    },1000)*/
        elem.css('margin-left', (ml + pixels) + 'px');
  }

Fiddle https://jsfiddle.net/uvsb6asa/3/

You added the transition to the wrong one:

working fiddle (has an issue with the initial next/back): https://jsfiddle.net/uvsb6asa/6/

Yours:

.outer ul {
    width: 600px;
    margin: 0 auto 0 auto;
}

.outer li {
    display: inline-block;
    -webkit-transition: margin-left 2s; /* Safari */
    transition: margin-left 2s;
}

Correct:

.outer ul {
    width: 600px;
    margin: 0 auto 0 auto;
    -webkit-transition: margin-left 2s; /* Safari */
    transition: margin-left 2s;
}

.outer li {
    display: inline-block;
}

The problem is you are adding transition to li but you are changing margin-left of ul.

You are doing:

.outer li {
    display: inline-block;
    -webkit-transition: margin-left 2s; /* Safari */
    transition: margin-left 2s;
}

Instead do this and it will work:

.outer li {
    display: inline-block;

}

.outer ul {
    -webkit-transition: margin-left 2s; /* Safari */
    transition: margin-left 2s;
}

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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