当我将它插入网站时,为什么这个滑块不起作用?

时间:2017-07-30 13:48:26

标签: javascript html css slider

我已经从codepen.io中选择了一个滑块放到我的网站上,当我嵌入它时它不起作用...应该分开的四个幻灯片都在一起,按钮不在#39;工作......当我在JSFiddle中粘贴代码时也会发生同样的事情......有人可以帮助我吗?

这是小提琴:https://jsfiddle.net/swpch979/ 这是代码:

<div class="slider">
  <input class="slider__nav" type="radio" name="slider" title="slide1" checked="checked"/>
  <input class="slider__nav" type="radio" name="slider" title="slide2"/>
  <input class="slider__nav" type="radio" name="slider" title="slide3"/>
  <input class="slider__nav" type="radio" name="slider" title="slide4"/>
  <div class="slider__inner">
    <div class="slider__contents"><i class="slider__image fa fa-codepen"></i>
      <h2 class="slider__caption">codepen</h2>
      <p class="slider__txt">Lorem ipsum dolor sit amet, consectetur adipisicing elit. At cupiditate omnis possimus illo quos, corporis minima!</p>
    </div>
    <div class="slider__contents"><i class="slider__image fa fa-newspaper-o"></i>
      <h2 class="slider__caption">newspaper-o</h2>
      <p class="slider__txt">Lorem ipsum dolor sit amet, consectetur adipisicing elit. At cupiditate omnis possimus illo quos, corporis minima!</p>
    </div>
    <div class="slider__contents"><i class="slider__image fa fa-television"></i>
      <h2 class="slider__caption">television</h2>
      <p class="slider__txt">Lorem ipsum dolor sit amet, consectetur adipisicing elit. At cupiditate omnis possimus illo quos, corporis minima!</p>
    </div>
    <div class="slider__contents"><i class="slider__image fa fa-diamond"></i>
      <h2 class="slider__caption">diamond</h2>
      <p class="slider__txt">Lorem ipsum dolor sit amet, consectetur adipisicing elit. At cupiditate omnis possimus illo quos, corporis minima!</p>
    </div>
  </div>
</div>
<style type="text/css">
@import url(https://fonts.googleapis.com/css?family=Josefin+Sans:400,700);
@import url(https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css);

$slider-length: 4;
$radioBtn-size: 12px;
$btn-color: #ccc;
$active-color: #333;
//---------------------------------------------------

*, *:before, *:after { box-sizing: border-box;}

html,body { height: 100%;}

body {
    color: #444;
    font-family: 'Josefin Sans', sans-serif;
    font-size: 1rem;
    line-height: 1.5;
}

.slider {
    height: 100%;
    position: relative;
    overflow: hidden;
    display: flex;
    flex-flow: row nowrap;
    align-items: flex-end;
    justify-content: center;

    &__nav {
        width: $radioBtn-size;
        height: $radioBtn-size;
        margin: 2rem $radioBtn-size;
        border-radius: 50%;
        z-index: 10;
        outline: $radioBtn-size / 2 solid $btn-color;
        outline-offset: $radioBtn-size / -2;
        box-shadow: 
            0 0 0 0 $active-color, 
            0 0 0 0 rgba($active-color,0);
        cursor: pointer;
        appearance: none;
        backface-visibility: hidden;

        &:checked {
            animation: check 0.5s linear forwards;

            @for $i from 0 to $slider-length {
                &:nth-of-type(#{$i+1}) {
                ~ .slider__inner {
                    transform: translateX((-100% * $i) / $slider-length);
                    }
                }
            }
        }
    }

    &__inner {
        position: absolute;
        top: 0;
        left: 0;
        width: 100% * $slider-length;
        height: 100%;
        transition: all 1s ease-out;
        display: flex;
        flex-flow: row nowrap;
    }

    &__contents {
        height: 100%;
        padding: 2rem;
        text-align: center;
        display: flex;
        flex: 1;
        flex-flow: column nowrap;
        align-items: center;
        justify-content: center;
    }

    &__image {
        font-size: 2.7rem;
    }

    &__caption {
        font-weight: 700;
        margin: 2rem 0 1rem;
        text-shadow: 0 1px 1px rgba(0,0,0,0.1);
        text-transform: uppercase;
    }

    &__txt {
        color: #999;
        margin-bottom: 3rem;
        max-width: 300px;
    }
}



// animation ---------------------------

@keyframes check {
    50% {
        outline-color: $active-color;
        box-shadow: 
            0 0 0 $radioBtn-size $active-color, 
            0 0 0 $radioBtn-size*3 rgba($active-color,0.2);
    }
    100% {
        outline-color: $active-color;
        box-shadow: 
            0 0 0 0 $active-color, 
            0 0 0 0 rgba($active-color,0);
    }
}

谢谢,

汤姆

1 个答案:

答案 0 :(得分:1)

它使用scss作为预处理器。转换为常规CSS,它将工作。

&#13;
&#13;
@import url(https://fonts.googleapis.com/css?family=Josefin+Sans:400,700);
@import url(https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css);
*,
*:before,
*:after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  color: #444;
  font-family: 'Josefin Sans', sans-serif;
  font-size: 1rem;
  line-height: 1.5;
}

.slider {
  height: 100%;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-flow: row nowrap;
  align-items: flex-end;
  justify-content: center;
}

.slider__nav {
  width: 12px;
  height: 12px;
  margin: 2rem 12px;
  border-radius: 50%;
  z-index: 10;
  outline: 6px solid #ccc;
  outline-offset: -6px;
  box-shadow: 0 0 0 0 #333, 0 0 0 0 rgba(51, 51, 51, 0);
  cursor: pointer;
  appearance: none;
  backface-visibility: hidden;
}

.slider__nav:checked {
  animation: check 0.5s linear forwards;
}

.slider__nav:checked:nth-of-type(1)~.slider__inner {
  transform: translateX(0%);
}

.slider__nav:checked:nth-of-type(2)~.slider__inner {
  transform: translateX(-25%);
}

.slider__nav:checked:nth-of-type(3)~.slider__inner {
  transform: translateX(-50%);
}

.slider__nav:checked:nth-of-type(4)~.slider__inner {
  transform: translateX(-75%);
}

.slider__inner {
  position: absolute;
  top: 0;
  left: 0;
  width: 400%;
  height: 100%;
  transition: all 1s ease-out;
  display: flex;
  flex-flow: row nowrap;
}

.slider__contents {
  height: 100%;
  padding: 2rem;
  text-align: center;
  display: flex;
  flex: 1;
  flex-flow: column nowrap;
  align-items: center;
  justify-content: center;
}

.slider__image {
  font-size: 2.7rem;
}

.slider__caption {
  font-weight: 700;
  margin: 2rem 0 1rem;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
  text-transform: uppercase;
}

.slider__txt {
  color: #999;
  margin-bottom: 3rem;
  max-width: 300px;
}

@keyframes check {
  50% {
    outline-color: #333;
    box-shadow: 0 0 0 12px #333, 0 0 0 36px rgba(51, 51, 51, 0.2);
  }
  100% {
    outline-color: #333;
    box-shadow: 0 0 0 0 #333, 0 0 0 0 rgba(51, 51, 51, 0);
  }
}
&#13;
<div class="slider">
  <input class="slider__nav" type="radio" name="slider" title="slide1" checked="checked" />
  <input class="slider__nav" type="radio" name="slider" title="slide2" />
  <input class="slider__nav" type="radio" name="slider" title="slide3" />
  <input class="slider__nav" type="radio" name="slider" title="slide4" />
  <div class="slider__inner">
    <div class="slider__contents"><i class="slider__image fa fa-codepen"></i>
      <h2 class="slider__caption">codepen</h2>
      <p class="slider__txt">Lorem ipsum dolor sit amet, consectetur adipisicing elit. At cupiditate omnis possimus illo quos, corporis minima!</p>
    </div>
    <div class="slider__contents"><i class="slider__image fa fa-newspaper-o"></i>
      <h2 class="slider__caption">newspaper-o</h2>
      <p class="slider__txt">Lorem ipsum dolor sit amet, consectetur adipisicing elit. At cupiditate omnis possimus illo quos, corporis minima!</p>
    </div>
    <div class="slider__contents"><i class="slider__image fa fa-television"></i>
      <h2 class="slider__caption">television</h2>
      <p class="slider__txt">Lorem ipsum dolor sit amet, consectetur adipisicing elit. At cupiditate omnis possimus illo quos, corporis minima!</p>
    </div>
    <div class="slider__contents"><i class="slider__image fa fa-diamond"></i>
      <h2 class="slider__caption">diamond</h2>
      <p class="slider__txt">Lorem ipsum dolor sit amet, consectetur adipisicing elit. At cupiditate omnis possimus illo quos, corporis minima!</p>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;