Display automatically set to block in JavaScript function

Giorgio Antonioli

I have a problem in a JavaScript function in which i handle slider properties. I have 3 slide with class .slide and everytime i "choose" one of them i set it with .active-slide class. The problem is that also if the display property of active-slide is table, when i set this class, it automatically changes to block and i can't understand why. P.s. i'm using also bootstrap. I'm posting here the part of my code related to this problem:

HTML:

<div class="slider">
        <div class="slide active-slide">        
            <div class="container up">
                <div class="row">
                    <div class="col-md-5 slide-text">
                        <h1 class="slide-title">whatuwant</h1>
                        <p class="slide-par">whatuwant</p>
                        <a href="#" class="slide-link">whatuwant</a>
                    </div>
                    <div class="col-md-7" id="first-slide-image">
                        <img src="whatuwant"/>
                    </div>
                </div>
            </div>
        </div>

        <div class="slide">        
            <div class="container up">
                <div class="row">
                    <div class="col-md-5 slide-text"> 
                        <h1 class="slide-title">whatuwant</h1>
                        <p class="slide-par">whatuwant</p>
                    </div>
                    <div class="col-md-7" id="second-slide-image">
                        <img src="whatuwant"/>
                    </div>
                </div>
            </div>
        </div>

        <div class="slide">        
            <div class="container up">
                <div class="row">
                    <div class="col-md-5 slide-text">
                        <h1 class="slide-title">whatuwant</h1>
                        <p class="slide-par">whatuwant</p>
                    </div>
                    <div class="col-md-7 slide-image" id="third-slide-image">
                        <img src="whatuwant"/>
                    </div>
                </div>
            </div>
        </div>
    </div>

CSS:

.container {
    width: 1024px;    
}

.up{  
    display: table-cell;
    vertical-align: middle;  
    max-width: 1024px;
    max-height: 470px;
}

.row{
    width: 1024px;
    margin: 0 auto;
    max-width: 1024px;
}

.slider {
    position: relative;
    width: 100%;
    height: 470px;
}

.slide {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

.active-slide{
    display: table;   
}

JavaScript:

$(document).ready(function() {
 $('.buttonfornextslide').click(function() {
    var currentSlide = $('.active-slide');
    var nextSlide = currentSlide.next();

    if(nextSlide.length === 0) {
      nextSlide = $('.slide').first();
    }

    currentSlide.fadeOut(600).removeClass('active-slide');
    nextSlide.fadeIn(600).addClass('active-slide');
  });


  $('.buttonforpreviousslide').click(function() {
    var currentSlide = $('.active-slide');
    var prevSlide = currentSlide.prev();

    if(prevSlide.length === 0) {
      prevSlide = $('.slide').last();
    }

    currentSlide.fadeOut(600).removeClass('active-slide');
    prevSlide.fadeIn(600).addClass('active-slide');
  });
});

I tried to simplify it to the maximum i could. If the question wasn't clear: how can i hold the .active-slide display property to table?

Rob Scott

jQuery automatically assigns "default" styles to elements when using fadeIn, slideDown, etc. Since the default style for divs are "block", that's what is being applied. Instead of using the class "active-slide", try specifically setting the CSS

....
prevSlide.fadeIn(600).css('display','table');

Refs:

jQuery bug

Fiddle - you can see that "display" block was in the class' style, and you have to literally add it after the fadeIn function.

In the fiddle, add

.css('display','block');

and run it, you'll see the changes

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to automatically switch off default display and set resolution if I connect a specific display?

분류에서Dev

Set time to execute Javascript function?

분류에서Dev

Positioning issues with display block

분류에서Dev

display <select> as a inline-block

분류에서Dev

How to automatically execute a javascript function from an external js file on page load with a specific condition

분류에서Dev

Automatically set an updatedAt time on a record

분류에서Dev

$HOME environment variable is not automatically set

분류에서Dev

How to display drupal 8 block in twig template

분류에서Dev

JQuery.css("display") = "block" not working

분류에서Dev

Automatically take screenshot of specific display in X

분류에서Dev

How to block/disable all videos from automatically loading in Chrome?

분류에서Dev

JavaScript "style.display = 'block'이 내 요소의 너비를 변경하는 이유는 무엇입니까?

분류에서Dev

How to set folder icons of multiple folders automatically?

분류에서Dev

How to set CORS of a public Google Cloud Storage buckets to avoid errors when using javascript fetch function?

분류에서Dev

GCC incorrectly inlining function with asm block

분류에서Dev

block only one function from inherited class

분류에서Dev

setInterval function in AngularJS controller is being called automatically

분류에서Dev

기본적으로 display : inline-block?

분류에서Dev

"Chaining" blocks of different heights with display:inline-block

분류에서Dev

display : inline-block 사용시 CSS 문제

분류에서Dev

display : inline-block 사용시 CSS 문제

분류에서Dev

Centralize div with display inline-block and position relative

분류에서Dev

Why setting display:none to an element inside inline-block div make inline-block not render as an inline-block;

분류에서Dev

How can I have an application automatically display on all desktops with openbox?

분류에서Dev

Pass a function with a set argument

분류에서Dev

set variable to an instance of a function

분류에서Dev

menuToggle CSS Problems - will not display or function

분류에서Dev

Javascript [virus?] added automatically on every page

분류에서Dev

how to check an inline function was automatically converted to a regular function by the compiler?

Related 관련 기사

  1. 1

    How to automatically switch off default display and set resolution if I connect a specific display?

  2. 2

    Set time to execute Javascript function?

  3. 3

    Positioning issues with display block

  4. 4

    display <select> as a inline-block

  5. 5

    How to automatically execute a javascript function from an external js file on page load with a specific condition

  6. 6

    Automatically set an updatedAt time on a record

  7. 7

    $HOME environment variable is not automatically set

  8. 8

    How to display drupal 8 block in twig template

  9. 9

    JQuery.css("display") = "block" not working

  10. 10

    Automatically take screenshot of specific display in X

  11. 11

    How to block/disable all videos from automatically loading in Chrome?

  12. 12

    JavaScript "style.display = 'block'이 내 요소의 너비를 변경하는 이유는 무엇입니까?

  13. 13

    How to set folder icons of multiple folders automatically?

  14. 14

    How to set CORS of a public Google Cloud Storage buckets to avoid errors when using javascript fetch function?

  15. 15

    GCC incorrectly inlining function with asm block

  16. 16

    block only one function from inherited class

  17. 17

    setInterval function in AngularJS controller is being called automatically

  18. 18

    기본적으로 display : inline-block?

  19. 19

    "Chaining" blocks of different heights with display:inline-block

  20. 20

    display : inline-block 사용시 CSS 문제

  21. 21

    display : inline-block 사용시 CSS 문제

  22. 22

    Centralize div with display inline-block and position relative

  23. 23

    Why setting display:none to an element inside inline-block div make inline-block not render as an inline-block;

  24. 24

    How can I have an application automatically display on all desktops with openbox?

  25. 25

    Pass a function with a set argument

  26. 26

    set variable to an instance of a function

  27. 27

    menuToggle CSS Problems - will not display or function

  28. 28

    Javascript [virus?] added automatically on every page

  29. 29

    how to check an inline function was automatically converted to a regular function by the compiler?

뜨겁다태그

보관