根据CSS中的浏览器大小更改背景颜色

时间:2016-05-27 04:55:33

标签: html css media-queries background-color

我无法弄清楚这段代码有什么问题。背景颜色根本没有变化,整个时间都保持白色。

private func test() {

    let data = ["fetch order list", "fetch shipping addresses", "fetch wishlist", "fetch other info"]

    let fetchInfoTasks = data.map{ asyncTask($0) }.toObservable()
    let someTasks = fetchInfoTasks.merge().toArray()
    let result = login().flatMapLatest{ _ in someTasks }

    startTask
        .rx_tap
        .flatMapLatest{ result }
        .catchError{ error in
            .....error......
            return Observable.empty()
        }
        .subscribeNext{ tasks in
            .....all completed....
        }
        .addDisposableTo(disposeBag)
}

private func login()-> Observable<String> {
    return Observable.create{ observer in
        performClosure(afterDealy: 1, onMainQueue: false) {
            if arc4random() % 4 == 0 {
                observer.onNext("login finished")
                observer.onCompleted()
            } else {
                observer.onError(NSError(domain: "", code: -1, userInfo: [NSLocalizedDescriptionKey: "some error"]))
            }
        }
        return AnonymousDisposable{}
    }
}

private func asyncTask(name: String)-> Observable<String> {
    return Observable.create{ observer in
        let delay = Double(arc4random() % 6 + 1)
        performClosure(afterDealy: delay, onMainQueue: false) {
            observer.onNext(name)
            observer.onCompleted()
        }
        return AnonymousDisposable{}
    }
}

func performClosure(afterDealy delay: Double, onMainQueue mainQueueOrNot: Bool, action: dispatch_block_t) {
    let delayIntervals = Double(NSEC_PER_SEC) * delay
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delayIntervals))
    let queue = mainQueueOrNot ? dispatch_get_main_queue() : dispatch_get_global_queue(QOS_CLASS_UTILITY, 0)
    dispatch_after(time, queue, action)
}

4 个答案:

答案 0 :(得分:4)

尝试:

@media screen and (min-width: 0px) and (max-width: 400px) {
    body { background-color: red; }
}
@media screen and (min-width: 401px) and (max-width: 599px) {
    body { background-color: green; }
}
@media screen and (min-width: 600px) {
    body { background-color: blue; }
}

background-color属性现已分配给body

答案 1 :(得分:1)

您需要为CSS中的某个元素定义media query(例如body}。这是demo

试试这个:

h1 {
    position: absolute;
    text-align: center;
    width: 100%;
    font-size: 6em;
    font-family: Roboto;
    transition: all 0.5s;
  }

  @media screen and (min-width: 0px) and (max-width: 400px) {
    body{
      background-color: red;
    }
  }
  @media screen and (min-width: 401px) and (max-width: 599px) {
    body{
      background-color: green;
    }
  }
  @media screen and (min-width: 600px) {
    body{
      background-color: blue;
    }
  }

答案 2 :(得分:0)

 @media screen and (min-width: 0px) and (max-width: 400px) {
   body{ background-color: red; }
 }
 @media screen and (min-width: 401px) and (max-width: 599px) {
   body{ background-color: green; }
 }
 @media screen and (min-width: 600px) {
   body{ background-color: blue; }
 }

样式只适用于身体或任何类别

答案 3 :(得分:0)

h1 {
  position: absolute;
  text-align: center;
  width: 100%;
  transition: all 0.5s;
}

@media screen and (min-width: 0px) and (max-width: 400px) {
  body{
    background-color: red;
  }
}

@media screen and (min-width: 401px) and (max-width: 599px) {
  body{
    background-color: green;
  }
}
  
@media screen and (min-width: 600px) {
  body{
    background-color: blue;
  }
}