我想简化这段代码:
$(window).on('DOMMouseScroll scroll mousewheel keydown', function () {
if ($('#home-section').hasClass('cd-section visible')) {
$('.li-underline').removeClass('active');
} else if ($('#what-section').hasClass('cd-section visible')) {
$('.li-underline').removeClass('active');
$('.li-width:first-child > li > .li-underline').addClass('active');
} else if ($('#case-studies-section').hasClass('cd-section visible')) {
$('.li-underline').removeClass('active');
$('.li-width:nth-child(2) > li > .li-underline').addClass('active');
} else if ($('#team-section').hasClass('cd-section visible')) {
$('.li-underline').removeClass('active');
$('.li-width:nth-child(3) > li > .li-underline').addClass('active');
} else if ($('#clients-section').hasClass('cd-section visible')) {
$('.li-underline').removeClass('active');
$('.li-width:nth-child(4) > li > .li-underline').addClass('active');
} else if ($('#contact-section').hasClass('cd-section visible')) {
$('.li-underline').removeClass('active');
$('.li-width:nth-child(5) > li > .li-underline').addClass('active');
}
});
因为我基本上一遍又一遍地重复相同的功能。
这样的东西会起作用吗?:
var activeSection = $('#what-section', 'team-section');
$(window).on('DOMMouseScroll scroll mousewheel keydown', function () {
activeSection.hasClass('cd-section visible') {
$('.li-underline').removeClass('active');
}
答案 0 :(得分:0)
为这些元素添加一个新类并按其选择,所以:
import UIKit
let kProgressViewTag = 10000
let kProgressUpdateNotification = "kProgressUpdateNotification"
extension UINavigationController {
open override func viewDidLoad() {
super.viewDidLoad()
let progressView = UIProgressView(progressViewStyle: .bar)
progressView.tag = kProgressViewTag
self.view.addSubview(progressView)
let navBar = self.navigationBar
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[navBar]-0-[progressView]", options: .directionLeadingToTrailing, metrics: nil, views: ["progressView" : progressView, "navBar" : navBar]))
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[progressView]|", options: .directionLeadingToTrailing, metrics: nil, views: ["progressView" : progressView]))
progressView.translatesAutoresizingMaskIntoConstraints = false
progressView.setProgress(0.0, animated: false)
NotificationCenter.default.addObserver(self, selector: #selector(UINavigationController.didReceiveNotification(notification:)), name: NSNotification.Name(rawValue: kProgressUpdateNotification), object: nil)
}
var progressView : UIProgressView? {
return self.view.viewWithTag(kProgressViewTag) as? UIProgressView
}
func didReceiveNotification(notification:NSNotification) {
if let progress = notification.object as? ProgressNotification {
if progress.current == progress.total {
self.progressView?.setProgress(0.0, animated: false)
} else {
let perc = Float(progress.current) / Float(progress.total)
self.progressView?.setProgress(perc, animated: true)
}
}
}
}
class ProgressNotification {
var current: Int = 0
var total: Int = 0
}
}
您可以将多个类添加到标记中,如:
let notification = ProgressNotification()
notification.current = processedTaskCount
notification.total = totalTaskCount
DispatchQueue.main.async {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: kProgressUpdateNotification), object: notification)
}
然后选择它们:
$(window).on('DOMMouseScroll scroll mousewheel keydown', function () {
if ($('.newclass').hasClass('cd-section visible')) {
$(this).removeClass('active');
}