如何触发类的宽度样式到另一个?

时间:2016-11-07 19:36:40

标签: javascript jquery html html5

我的 HTML

中有这两个对象
//1st object
<span class="ui-slider-handle" tabindex="0" style="left: 15.3153%;"></span>

//2nd object
<div id="waveform">
  <wave style="display: block; position: relative; user-select: none; height: 50px; overflow-x: auto; overflow-y: hidden;">
      <canvas></canvas>
      <wave style="position: absolute; z-index: 2; left: 0px; top: 0px; bottom: 0px; overflow: hidden; width: 0px; display: block; box-sizing: border-box; border-right: 1px solid rgb(255, 255, 255);"><canvas width="350" height="50" style="width: 350px;"></canvas></wave>
  </wave>
</div>

是否可以使用Javascript / jQuery将ui-slider-handle类的“left”css属性的百分比连接到内部wave的宽度并始终触发百分比(第1个)对象)改变?

我的第一次尝试

$(document).ready(function(){

  var playlist = [{
      title:"Hidden",
      artist:"Miaow",
      mp3:"http://www.jplayer.org/audio/mp3/Miaow-02-Hidden.mp3",
      oga:"http://www.jplayer.org/audio/ogg/Miaow-02-Hidden.ogg",
      poster: "https://i.imgur.com/sCbrzQa.png"
    },{
      title:"Cro Magnon Man",
      artist:"The Stark Palace",
      mp3:"http://www.jplayer.org/audio/mp3/TSP-01-Cro_magnon_man.mp3",
      oga:"http://www.jplayer.org/audio/ogg/TSP-01-Cro_magnon_man.ogg",
      poster: "https://i.imgur.com/lXvsuBu.png"
    },{
      title:"Bubble",
      m4a: "http://www.jplayer.org/audio/m4a/Miaow-07-Bubble.m4a",
      oga: "http://www.jplayer.org/audio/ogg/Miaow-07-Bubble.ogg",
      poster: "https://i.imgur.com/klJKSVZ.jpg"
  }];
  
  var cssSelector = {
    jPlayer: "#jquery_jplayer",
    cssSelectorAncestor: ".music-player"
  };
  
  var options = {
    swfPath: "http://cdnjs.cloudflare.com/ajax/libs/jplayer/2.6.4/jquery.jplayer/Jplayer.swf",
    supplied: "ogv, m4v, oga, mp3",
    volumechange: function(event) {
      $( ".volume-level" ).slider("value", event.jPlayer.options.volume);
    },
    timeupdate: function(event) {
      $( ".progress" ).slider("value", event.jPlayer.status.currentPercentAbsolute);
    }
  };
  
  var myPlaylist = new jPlayerPlaylist(cssSelector, playlist, options);
  var PlayerData = $(cssSelector.jPlayer).data("jPlayer");
  
  
  // Create the volume slider control
  $( ".volume-level" ).slider({
     animate: "fast",
		max: 1,
		range: "min",
		step: 0.01,
		value : $.jPlayer.prototype.options.volume,
		slide: function(event, ui) {
			$(cssSelector.jPlayer).jPlayer("option", "muted", false);
			$(cssSelector.jPlayer).jPlayer("option", "volume", ui.value);
		}
  });
  
  // Create the progress slider control
  $( ".progress" ).slider({
		animate: "fast",
		max: 100,
		range: "min",
		step: 0.1,
		value : 0,
		slide: function(event, ui) {
			var sp = PlayerData.status.seekPercent;
			if(sp > 0) {
				// Move the play-head to the value and factor in the seek percent.
				$(cssSelector.jPlayer).jPlayer("playHead", ui.value * (100 / sp));
			} else {
				// Create a timeout to reset this slider to zero.
				setTimeout(function() {
					 $( ".progress" ).slider("value", 0);
				}, 0);
			}
		}
	});

  $( ".ui-slider-range" ).slider({
    change: function( event, ui ) {
      $('wave').css('width', ui.value)
    }
  });  

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

html {
  min-height: 100%;
}

body {
  background: #eee;
  font-family: "Open Sans", sans-serif;
}

.music-player {
  position: relative;
  width: 350px;
  height: 370px;
  margin: 50px auto;
  box-shadow: 0 0 60px rgba(0, 0, 0, 0.8);
  border-radius: 10px;
  background: #222;
  overflow: hidden;
  z-index: 0;
}
.music-player img {
  position: absolute;
  top: 0px;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: -1;
  display: block;
  width: 100% !important;
  height: 100% !important;
  -webkit-filter: blur(2px);
          filter: blur(2px);
}
.music-player .info {
  width: 100%;
  height: 100px;
  background: #222;
  background: rgba(0, 0, 0, 0.8);
  text-align: center;
  position: relative;
}
.music-player .info .jp-playlist li {
  display: none;
}
.music-player .info .jp-playlist li a {
  font-size: 30px;
  font-weight: 300;
  text-decoration: none;
  color: #fff;
  color: rgba(225, 225, 225, 0.4);
}
.music-player .info .jp-playlist li a span {
  font-size: 14px;
  display: block;
  margin-top: 10px;
}
.music-player .info .jp-playlist li.jp-playlist-current {
  display: block;
}
.music-player .info .jp-playlist li .jp-free-media, .music-player .info .jp-playlist li .jp-playlist-item-remove {
  display: none;
}
.music-player .info .left, .music-player .info .right {
  width: 25px;
  position: absolute;
  top: 30px;
  left: 30px;
}
.music-player .info .right {
  left: auto;
  right: 30px;
}
.music-player .info [class^="icon-"] {
  margin: 0 0 10px;
}
.music-player .info .center {
  padding: 20px 0 0;
}
.music-player .progress, .music-player .volume-level {
  width: 100%;
  height: 5px;
  display: block;
  background: #ccc;
  position: absolute;
  bottom: 0px;
  cursor: pointer;
  border: none;
}
.music-player .progress .ui-slider-range, .music-player .volume-level .ui-slider-range {
  display: block;
  background: #ed553b;
  height: 5px;
  border-radius: 0;
}
.music-player .progress .ui-slider-handle, .music-player .volume-level .ui-slider-handle {
  position: absolute;
  top: -8px;
  width: 8px;
  height: 22px;
  background: url("//i.imgur.com/tsqwz1N.png") no-repeat center;
  border: none;
  outline: none;
  margin: 0 0 0 -3px;
  cursor: move;
}
.music-player .controls {
  text-align: center;
  width: 100%;
  height: 190px;
  background: #982e4b;
  background: rgba(152, 46, 75, 0.6);
}
.music-player .controls .current {
  font-size: 48px;
  color: #fff;
  color: rgba(225, 225, 225, 0.4);
  padding: 15px 0 20px;
}
.music-player .controls .play-controls a {
  display: inline-block;
  width: 35px;
  height: 40px;
  margin: 0 30px;
}
.music-player .controls .volume-level {
  position: relative;
  bottom: auto;
  width: 200px;
  height: 2px;
  margin: 30px auto 0;
  background: rgba(225, 225, 225, 0.3);
}
.music-player .controls .volume-level .ui-slider-range {
  height: 2px;
}
.music-player .controls .volume-level .ui-slider-handle {
  top: -8px;
  margin-left: -9px;
  width: 22px;
  height: 22px;
  background-image: url("http://image.flaticon.com/icons/svg/106/106874.svg");
}
.music-player .controls .volume-level .icon-volume-up, .music-player .controls .volume-level .icon-volume-down {
  position: absolute;
  right: -34px;
  top: -8px;
  width: 22px;
}
.music-player .controls .volume-level .icon-volume-down {
  right: auto;
  left: -27px;
}

[class^="icon-"] {
  width: 18px;
  height: 18px;
  background: url("//i.imgur.com/E09T8tf.png") no-repeat center;
  display: block;
}

.icon-shuffle {
  background-image: url("http://image.flaticon.com/icons/svg/148/148739.svg");
}
.icon-heart {
  background-image: url("http://image.flaticon.com/icons/svg/126/126499.svg");
}
.icon-repeat {
  background-image: url("http://image.flaticon.com/icons/svg/137/137485.svg");
}
.icon-share {
  background-image: url("http://image.flaticon.com/icons/svg/189/189676.svg");
}
.icon-previous {
  background-image: url("http://image.flaticon.com/icons/svg/137/137518.svg");
}
.icon-play {
  background-image: url("http://image.flaticon.com/icons/svg/148/148744.svg");
}
.icon-pause {
  background-image: url("http://image.flaticon.com/icons/svg/189/189639.svg");
}
.icon-next {
  background-image: url("http://image.flaticon.com/icons/svg/137/137517.svg");
}
.icon-volume-up {
  background-image: url("http://image.flaticon.com/icons/svg/149/149139.svg");
}
.icon-volume-down {
  background-image: url("http://image.flaticon.com/icons/svg/109/109699.svg");
}
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  
  <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/jplayer/2.6.4/jquery.jplayer/jquery.jplayer.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/jplayer/2.6.4/add-on/jplayer.playlist.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.0.52/wavesurfer.min.js"></script>
</head>

<div class="music-player">
  <div style="background-image: url(https://image.freepik.com/free-vector/welcome-summer-badge-on-blurry-background_23-2147511412.jpg);" class="album"></div>

  <div class="info">
    
    <div class="left">
      <a href="javascript:;" class="icon-shuffle"></a>
      <a href="javascript:;" class="icon-heart"></a>
    </div>
    
    <div class="center">
      <div class="jp-playlist">
        <ul>
          <li></li>
        </ul>
      </div> 
    </div>
    
    <div class="right">
      <a href="javascript:;" class="icon-repeat"></a>
      <a href="javascript:;" class="icon-share"></a>
    </div>
    
    <div class="progress"></div>
    
  </div>
  <div class="demo">
  <div id="waveform">
  </div>

  <script>
    var ctx = document.createElement('canvas').getContext('2d');
    var linGrad = ctx.createLinearGradient(0, 64, 0, 200);
    linGrad.addColorStop(0.5, 'rgba(255, 255, 255, 1.000)');
    linGrad.addColorStop(0.5, 'rgba(183, 183, 183, 1.000)');

    var wavesurfer = WaveSurfer.create({
      container: '#waveform',
      waveColor: linGrad,
      progressColor: 'hsla(200, 100%, 30%, 0.5)',
      cursorColor: '#fff',
      normalize: true,
      backend: 'MediaElement',
      height:50,
      barWidth: 3
    });
//Set peaks
wavesurfer.backend.peaks = [0.0218, 0.0183, 0.0165, 0.0198, 0.2137, 0.2888, 0.2313, 0.15, 0.2542, 0.2538, 0.2358, 0.1195, 0.1591, 0.2599, 0.2742, 0.1447, 0.2328, 0.1878, 0.1988, 0.1645, 0.1218, 0.2005, 0.2828, 0.2051, 0.1664, 0.1181, 0.1621, 0.2966, 0.189, 0.246, 0.2445, 0.1621, 0.1618, 0.189, 0.2354, 0.1561, 0.1638, 0.2799, 0.0923, 0.1659, 0.1675, 0.1268, 0.0984, 0.0997, 0.1248, 0.1495, 0.1431, 0.1236, 0.1755, 0.1183, 0.1349, 0.1018, 0.1109, 0.1833, 0.1813, 0.1422, 0.0961, 0.1191, 0.0791, 0.0631, 0.0315, 0.0157, 0.0166, 0.0108];

//Draw peaks
wavesurfer.drawBuffer();
  </script>
  
  </div>    
  <div class="controls">
    <div class="current jp-current-time">00:00</div>
    <div class="play-controls">
      <a href="javascript:;" class="icon-previous jp-previous" title="previous"></a>
      <a href="javascript:;" class="icon-play jp-play" title="play"></a>
      <a href="javascript:;" class="icon-pause jp-pause" title="pause"></a>
      <a href="javascript:;" class="icon-next jp-next" title="next"></a>
    </div>
    <div class="volume-level">
      <a href="javascript:;" class="icon-volume-up" title="max volume"></a>
      <a href="javascript:;" class="icon-volume-down" title="mute"></a>
    </div>
  </div>
  
  <div id="jquery_jplayer" class="jp-jplayer"></div>
  
</div>

更新

上面的代码创建了2个不同的滑块选择器,滑块也会影响波形对象的宽度。而不是我需要这样的东西:

enter image description here

2 个答案:

答案 0 :(得分:1)

我假设您正在使用jQuery中的UI Slider。初始化后,您可以使用change事件,如下所示:

$( ".ui-slider-handle" ).slider({
  change: function( event, ui ) {
    $('wave').first().next().css('width', ui.value)
  }
});

编辑:我刚刚意识到你想要第二次浪潮,所以我添加了.next()

答案 1 :(得分:0)

您要找的是MutationObserver()。它允许您注册element属性中的任何更改并对其进行操作。

请务必检查浏览器支持,因为它很可能不包含您实际需要的内容。