如何修改一个没有jquery运行的小脚本

时间:2015-08-03 16:20:34

标签: javascript jquery css scrollbar

以下脚本(启用2个并行滚动条)有几行但需要jquery库,我怎样才能使它成为独立的javascript,提前感谢...

http://jsfiddle.net/TBnqw/1/

HTML:

<div class="wrapper1">
    <div class="div1">
    </div>
</div>
<div class="wrapper2">
    <div class="div2">
    aaaa bbbb cccc dddd aaaa bbbb cccc dddd aaaa bbbb cccc dddd aaaa bbbb cccc dddd aaaa bbbb cccc dddd aaaa bbbb cccc dddd aaaa bbbb cccc dddd
    </div>
</div>

JAVASCRIPT:

$(function(){
    $(".wrapper1").scroll(function(){
        $(".wrapper2")
            .scrollLeft($(".wrapper1").scrollLeft());
    });
    $(".wrapper2").scroll(function(){
        $(".wrapper1")
            .scrollLeft($(".wrapper2").scrollLeft());
    });
});

CSS:

.wrapper1, .wrapper2{width: 300px; border: none 0px RED;
overflow-x: scroll; overflow-y:hidden;}
.wrapper1{height: 20px; }
.wrapper2{height: 200px; }
.div1 {width:1000px; height: 20px; }
.div2 {width:1000px; height: 200px; background-color: #88FF88;
overflow: auto;}

2 个答案:

答案 0 :(得分:4)

这是:

var scrollBarTop = document.querySelector(".wrapper1");
var scrollBarBottom = document.querySelector(".wrapper2");

scrollBarTop.addEventListener("scroll", function(){
    scrollBarBottom.scrollLeft = scrollBarTop.scrollLeft;    
});

scrollBarBottom.addEventListener("scroll", function(){
    scrollBarTop.scrollLeft = scrollBarBottom.scrollLeft;    
});

答案 1 :(得分:0)

这是Angular 2 / Angular的做法。即没有Jquery

奇怪的是,你不能只设置变量,而是必须直接对文档进行

https://plnkr.co/edit/eYz2c8?p=preview

import { Component, ViewEncapsulation } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  encapsulation: ViewEncapsulation.Emulated
 })
export class AppComponent {

onWindowsScrollTop() {
  document.querySelector('.table-wrapper-bottom').scrollLeft = document.querySelector('.table-wrapper-top').scrollLeft;
}
onWindowsScrollBottom() {
  document.querySelector('.table-wrapper-top').scrollLeft = document.querySelector('.table-wrapper-bottom').scrollLeft;
}

reportTopScrollLeft() {
  alert('the scrollLeft for top = ' + document.querySelector('.table-wrapper-top').scrollLeft)
}

reportBottomScrollLeft() {
  alert('the scrollLeft for top = ' + document.querySelector('.table-wrapper-bottom').scrollLeft)
}

 }